Search code examples
vue.jsvuejs3hello.js

Emitting value from VUE 3 setup() listener


I have a listener on setup() lifecycle method

setup() {
    const showLogout = ref(false)
    hello.on('auth.login', function(auth) {
               this.$emit("auth", auth);
            })
    return { showLogout, hello }
  }

How to can I get this emit event inside parent component. It is working when I put emit code into normal method, but when it's inside the listener here it's not working.

Any other place I could put this listener into?


Solution

  • setup(props,context) {
        const showLogout = ref(false)
        hello.on('auth.login', function(auth) {
                   context.emit("auth", auth);
                })
        return { showLogout, hello }
      }
    

    OR

    setup(props,{emit}) {
        const showLogout = ref(false)
        hello.on('auth.login', function(auth) {
                 emit("auth", auth);
                })
        return { showLogout, hello }
      }
    

    you need to use emit this way

    <component @authFunction='auth'/>