I
I
ivan05122020-08-04 20:04:06
Vue.js
ivan0512, 2020-08-04 20:04:06

Is there any difference for Vue how to set event listeners?

I have always considered it correct to set listeners in this way @event="method"
However, I often see this variant @event="method()". Do I understand correctly that Vue is smart and wraps such a method itself?
If there is a need to immediately pass an additional argument, that is, @click = "method (x)", then the view is able to wrap it correctly so that a function is not created every time?

These questions appeared after working with React, where you always need to use useCallback so that the function does not recreate every time

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-08-05
@Fragster

if without brackets, then the function will be called with default arguments, if with brackets, then it will be an arbitrary expression in which the $event object with the native js event and the entire context of the component and / or v-for loops will be available, if this piece is displayed in loop (or nested loops).
If this is an arbitrary event from a nested component with a value passing, then you can do this: @event="payload => {alert (payload)}" for an arbitrary expression. Really useful for working in the context (with access to a particular collection element) of a v-for loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question