【问题标题】: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'/>
    

    【讨论】:

    • 谢谢,慢慢开始弄VUE 3
    • 欢迎您。一个月前我遇到了这个问题。没关系
    猜你喜欢
    • 2021-02-03
    • 2021-02-09
    • 2022-01-19
    • 2018-10-12
    • 2018-03-22
    • 1970-01-01
    • 2020-05-23
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多