首页 > 其他 > 详细

provide 父传子 祖传孙 并且 子改变父 孙改变祖

时间:2021-06-07 09:20:54      阅读:27      评论:0      收藏:0      [点我收藏+]

祖先组件

 

<template>
我是head{{title}}
<v-head/>
</template>

<script>
import VHead from ‘./components/head.vue‘;
import {provide, ref} from "vue";
export default {
name: ‘App‘,
components: {
VHead,
},
data () {
return {
‘msg‘: ‘123‘,
}
},
setup() {
let title = ref(‘我是head的title‘);

provide(‘title‘, title);
return {
title
}

}
}
</script>

子孙组件

<template>
<section>
123{{ abc }}

<button @click="changeTitle">footer的提交</button>
</section>
</template>

<script>
import { inject } from ‘vue‘;
export default {
name: "footer",
setup () {
let abc = inject(‘title‘);

let changeTitle = () => {
abc.value = ‘123‘
}
return{
abc,
changeTitle
}
}
}
</script>

provide 父传子 祖传孙 并且 子改变父 孙改变祖

原文:https://www.cnblogs.com/anans/p/14857116.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!