Preface: When passing data between parent and child components, props and emit are usually used. When passing data from parent to child, props is used. If the parent component passes it to the grandchild component, it needs to be passed to the child component first, and then the child component passes it to the grandchild component. If multiple child components or multiple grandchild components are used, it needs to be passed many times, which will be very troublesome. In cases like this, you can use 1. Use of provide / inject in Vue2provide: is an object containing properties and values. like: provide:{ info:"value" } If provide(){ return { info: this.msg } } inject: [ 'info' ] Receives the info data provided by provide above, which can also be an object containing from and default attributes. From is the key used to search in the injected content, and the default attribute is to specify the default value. In vue2 project/inject application: //Parent component export default{ provide:{ info:"Provide data" } } //Subcomponent export default{ inject:['info'], mounted(){ console.log("Receive data:", this.info) // Receive data: Provide data} } 2. Use of provide / inject in Vue3 Use The provide function receives two parameters:
When using: import { provide } from "vue" export default { setup(){ provide('info',"value") } } The inject function takes two parameters:
When using: import { inject } from "vue" export default { setup(){ inject('info',"Set default value") } } Complete example 1 : //Parent component code <script> import { provide } from "vue" export default { setup(){ provide('info',"value") } } </script> //Subcomponent code <template> {{info}} </template> <script> import { inject } from "vue" export default { setup(){ const info = inject('info') return { info } } } </script> 3. Add responsiveness To add responsiveness to Complete Example 2 : //Parent component code <template> <div> info:{{info}} <InjectCom ></InjectCom> </div> </template> <script> import InjectCom from "./InjectCom" import { provide,readonly,ref } from "vue" export default { setup(){ let info = ref("Have you studied today?") setTimeout(()=>{ info.value = "Don't make excuses, start learning now" },2000) provide('info',info) return { info } }, components:{ InjectCom } } </script> // InjectCom subcomponent code <template> {{info}} </template> <script> import { inject } from "vue" export default { setup(){ const info = inject('info') setTimeout(()=>{ info.value = "Update" },2000) return { info } } } </script> In the above example, the value of When modifying the value in the subscription component, it can be modified normally. If other components also use the value, the status is likely to be confused, so it is necessary to avoid the problem at the source. Directions: import { readonly } from "vue" let info = readonly('read-only info value') setTimout(()=>{ info="Update info" //Update the value of info after two seconds},2000) After running for two seconds, the browser issues a warning, indicating that the info value cannot be modified. So we add a read-only attribute to the data emitted by Add readonly to provide in the complete example 2. provide('info', readonly(info)) When a subcomponent modifies the value, there will be a read-only reminder. When modifying the value, you still need to modify the data in the component that like: //Publish let info = ref("Have you studied today?") const changeInfo = (val)=>{ info.value = val } provide('info',readonly(info)) provide('changeInfo',changeInfo) //Subscription const chang = inject('changeInfo') chang('rush to front-end engineer') Complete Example 3: Modifying Data
// Parent component code <template>
<div>
info:{{info}}
<InjectCom ></InjectCom>
</div>
</template>
<script>
import InjectCom from "./InjectCom"
import { provide,readonly,ref } from "vue"
export default {
setup(){
let info = ref("Have you studied today?")
const changeInfo = (val)=>{
info.value = val
}
provide('info',readonly(info))
provide('changeInfo',changeInfo)
return {
info
}
},
components:{
InjectCom
}
}
</script>
//InjectCom subcomponent code <template>
<div>
<button @click="chang('Rush to the front-end engineer')">Update value</button>
</div>
</template>
<script>
import { inject } from "vue"
export default {
setup(){
const info = inject('info')
const chang = inject('changeInfo')
return {
info,
Chang
}
}
}
</script>
This is the end of this article about the usage and principles of You may also be interested in:
|
<<: CSS Viewport Units for Fast Layout
>>: How to create a stylish web page design (graphic tutorial)
First of all, what is a font icon? On the surface...
Preface Execute the show create table <tablena...
Table of contents Preface optimization SSR Import...
I have previously shared the usage of asynchronou...
Table of contents 1. Effect display 2. Enhanced v...
Ubuntu is a relatively popular Linux desktop syst...
Table of contents 1. Use RGB to represent color 2...
There are two common ways to make div background ...
The author recently encountered a performance bot...
I have just come into contact with and become fam...
Table of contents 1. Brief Introduction 2. Run sc...
Thanks to the development of the Internet, we can...
Introduction to vi/vim They are both multi-mode e...
Let's first look at the definition of the pos...
MYSQL is case sensitive Seeing the words is belie...