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)
1. If MySQL is not started successfully, check th...
illustrate: VMware IOInsight is a tool that helps...
Preface What is data type conversion? The default...
Code: Copy code The code is as follows: <!DOCTY...
Table of contents 1. Introduction to import_table...
Table of contents Update the image from an existi...
Solve the problem of Chinese garbled characters i...
Detailed description of properties The purpose of...
0 Differences between icons and images Icons are ...
Part 3: ❤Three ways to overlook backend data rece...
When the page is not responding, displaying the l...
Table of contents 1. Routing animation 2. Group Q...
When we learn HTML, the image tag <img> int...
MySQL allows you to create multiple indexes on th...
Preface Learn MySQL to reorganize previous non-MK...