在组件中emit使用方法:
父组件:
<Dialog @change="change"/>
setup(){
const change=(value)=>{
}
}
子组件使用:
setup(props, { emit }) {
const close = ()=>{
emit("change")
}
return{
close
}
}
或者
setup(props, context){
const { emit } = context;
const close = ()=>{
emit("change")
}
return{
close
}
}
或者
import {getCurrentInstance} from "vue"
setup(){
const {global} = getCurrentInstance();
const close = ()=>{
global.$emit('change')
}
return{
close
}
}
本文详细介绍了在Vue组件中如何使用emit进行父子通信。从父组件监听子组件事件到子组件触发事件,展示了setup语法糖下的不同实现,包括通过props的第二个参数、上下文对象以及利用getCurrentInstance获取全局实例的方法。这些技巧对于理解和优化Vue组件通信至关重要。
5389

被折叠的 条评论
为什么被折叠?



