爷爷组件
<template>
{{title}}
<button @click="setTitle">改变title</button>
<br>
<Son />
</template>
<script>
import Son from "./components/Son"
export default {
components:{
Son
},
provide(){
return{
title:this.title,
userInfo:{
name:"张三",
age:40
}
}
},
methods:{
setTitle(){
this.title = "修改后的Title"
console.log(this.title)
}
}
}
</script>
<style>
</style>
父组件
<template>
<Grandson />
</template>
<script>
import Grandson from "./Grandson"
export default {
components:{
Grandson
}
}
</script>
<style>
</style>
孙子组件
<template>
孙子 {{title}}
<br/>
{{userInfo}}
</template>
<script>
export default {
inject:["title","userInfo"]
}
</script>
<style>
</style>