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)
01. Command Overview The seq command is used to g...
Effect picture: Implementation code: <template...
If there is a backup, it is very simple. You only...
This article shares the specific code of js+canva...
Table of contents 1. Use the warehouse to create ...
This article introduces the sample code of CSS to...
Table of contents chmod Example Special attention...
1. Implement a simple triangle Using the border i...
Table of contents Preface Introduction JavaScript...
Solution to forgetting MySQL password: [root@loca...
This article mainly introduces how to specify par...
The TextBox with the ReadOnly attribute will be di...
The complete syntax of the select statement is: S...
When I was writing a project yesterday, I needed ...
This article example shares the specific code of ...