Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `emit` event out of `setup` method in vue3?

Tags:

vue.js

vuejs3

I know I can call the emit method from the setup method, but is there any way to emit event from any other functions without passing the emit method from setup method(not the the functions in the methods option, but a useXXX function) ?

like image 971
yaquawa Avatar asked Jan 10 '21 11:01

yaquawa


1 Answers

setup function takes two arguments, First one is props. And the second one is context which exposes three component properties, attrs, slots and emit.

You can access emit from context like:

export default {
    setup(props, context) {
        context.emit('event');
    },
};

or

export default {
    setup(props, { emit }) {
        emit('event');
    },
};

Source

like image 142
B45i Avatar answered Sep 28 '22 12:09

B45i