1.祖孙组件传值(provide、inject )
祖组件
import { ref, provide} from "vue";
const navName = ref('瞌睡的博客主页')
const sendGrandson = function (value) {
navName.value = value
}
provide('sendHandle', sendGrandson)
孙组件
import { ref,inject } from 'vue';
const sendGrandson = inject('sendHandle');
const getNavName = function (params) {
sendGrandson(params)
}
2.父子组件双向绑定
子组件
import { ref,reactive, watch,defineEmits} from 'vue';
let str = ref()
const emit = defineEmits(['update:getValue'])
emit('update:getValue',str.value);
父组件
<NavLabel v-model:getValue="getValue"></NavLabel>
const getValue = ref()
watch(getValue,(val)=>{
console.log(getValue.value,'获取到的值');
})
![在这里插入图片描述](https://img-blog.csdnimg.cn/443ca53878464e9990796484b36b995a.png)
3.父子组件传值
父组件
<NavLabel :foo="getValue"></NavLabel>
const getValue = ref('321')
子组件
const props = defineProps(['foo'])
console.log(props.foo)
4.子组件执行父组件方法
父组件
<NavLabel @getValue="getValue"></NavLabel>
const getValue = function(data){
console.log(data,'子组件执行父组件方法');
}
子组件
import { defineProps,defineEmits ,inject } from 'vue';
const emit = defineEmits(['getValue'])
const getNavName = function (params) {
emit('getValue',321)
}