创建三个组件GrandpaA.vue (爷) => FatherA.vue (父) => SonA (儿)
在每个组件中套用下一个组件
爷组件
<template>
<div class="box">
GrandpaA 爷爷
<button @click="change">修改数据</button>
<FatherA></FatherA>
</div>
</template>
<script>
import FatherA from "@/components/FatherA.vue";
export default {
provide() {
return {
color: this.color,
userInfo: this.userInfo,
};
},
data() {
return {
color: "pink", //简单数据类型(不是响应性)
userInfo: {
name: "张三", //复杂数据类型(响应性)
age: 18,
},
};
},
methods:{
change(){
// 点击修改数据
// this.color = "red"; //没有响应性
this.userInfo.name = "李四"; //有响应性
}
},
components: {
FatherA,
}
}
</script>
孙祖件
<template>
<div class="box">
<p>SonA 儿子</p>
{{ color }}
{{ userInfo.name }}
{{ userInfo.age }}
</div>
</template>
<script>
export default {
inject: ["color", "userInfo"],
};
</script>
流程图