2

Provide/inject provides us with a way to pass values between components, but by default, provide/inject binding is not reactive. In official words:

image

Then, we can achieve data responsiveness by passing an object.

//父组件Father.vue

 data(){
     return {
        obj: {            //一定是个对象,才能实现响应式
             name: 'eavan'
         }
     }
 },
 provide() {            //要访问组件实例 property,我们需要将 provide 转换为返回对象的函数
     return{
         user: this.obj
    }
 }
//子组件Child.vue

export default {
    name: 'Child',
    inject: ['user']    //正常使用inject
}

这个少年有点热丶
363 声望12 粉丝

[链接]