问题:在负组件中调用子组件方法找不到值
父组件:1.定义ref
<history-data ref="child" ></history-data>
2.定义与ref名字一样的值,并在return中返回
const child = ref();
const onSearch = () => {
child.value.child1()
}
return {
child
}
子组件:1.引入defineExpose方法
import { ref, onMounted, nextTick, reactive,defineExpose } from 'vue';
2.定义方法并通过defineExpose方法暴漏出去,并在return中返回
const child1 = () => {
console.log(1111)
}
defineExpose({
child1,
})
return {
child1,
};
注意:在父组件中引入子组件不要使用v-if,否则打印的value值为undefined。