Robert
Robert

Reputation: 32755

Is there any way to call more than one function from a vue.js directive?

Is there a way to call more than one function from a vue.js directive?

I know that I could create a function that calls the other two.

What I would like to know if I can call, from the html file, two functions for the same event.

For instance:

<input type="text"
       v-on:keyup.enter="myFirstFunction()"
       v-on:keyup.enter="mySecondFunction()"    
>
</input>

Unfortunately, it seems that this only bind the first function.

Upvotes: 0

Views: 480

Answers (3)

Robert
Robert

Reputation: 32755

<input type="text"
       v-on:keyup.enter="myFirstFunction() & mySecondFunction()"
>
</input>

Upvotes: 0

Linus Borg
Linus Borg

Reputation: 23968

this cannot really work since directives like v-on:keyup are simply element attributes to the browser, and you can't have duplicate attributes on an element.

So yes, you will have to create a method that calls the other two.

The functions are not related between them and they are not used in the same combination through the code, so, it doesn't make sense add code that composes them

I don't see it that way. In this scenario, the functions obvously are related - they are called from the same event. a simple two-line method solves the issue.

Upvotes: 1

Henry Letting
Henry Letting

Reputation: 146

Try calling your second function in your first function instead, or make a third function that has calls both functions in it, then call it. This is because the event is only recognized once.

Upvotes: 0

Related Questions