【问题标题】:Emitting value from VUE 3 setup() listener从 VUE 3 setup() 监听器发出值
【发布时间】:2021-09-23 12:18:48
【问题描述】:
我在 setup() 生命周期方法上有一个监听器
setup() {
const showLogout = ref(false)
hello.on('auth.login', function(auth) {
this.$emit("auth", auth);
})
return { showLogout, hello }
}
如何在父组件中获取此发出事件。当我将发射代码放入普通方法时它可以工作,但是当它在这里的侦听器内部时它不起作用。
我可以把这个监听器放到其他地方吗?
【问题讨论】:
标签:
vue.js
vuejs3
hello.js
【解决方案1】:
setup(props,context) {
const showLogout = ref(false)
hello.on('auth.login', function(auth) {
context.emit("auth", auth);
})
return { showLogout, hello }
}
或
setup(props,{emit}) {
const showLogout = ref(false)
hello.on('auth.login', function(auth) {
emit("auth", auth);
})
return { showLogout, hello }
}
你需要以这种方式使用发射
<component @authFunction='auth'/>