Difference between @click and v-on:click Vuejs

vue.jsVue Directives

vue.js Problem Overview


The questions should be enough clear.

But I can see that someone use:

<button @click="function()">press</button>

Someone use:

<button v-on:click="function()">press</button>

But really what is the difference between the two (if exists)

vue.js Solutions


Solution 1 - vue.js

There is no difference between the two, one is just a shorthand for the second.

> The v- prefix serves as a visual cue for identifying Vue-specific > attributes in your templates. This is useful when you are using Vue.js > to apply dynamic behavior to some existing markup, but can feel > verbose for some frequently used directives. At the same time, the > need for the v- prefix becomes less important when you are building an > SPA where Vue.js manages every template.

<!-- full syntax -->
<a v-on:click="doSomething"></a>
<!-- shorthand -->
<a @click="doSomething"></a>

Source: official documentation.

Solution 2 - vue.js

v-bind and v-on are two frequently used directives in vuejs html template. So they provided a shorthand notation for the both of them as follows:

You can replace v-on: with @

v-on:click='someFunction'

as:

@click='someFunction'

Another example:

v-on:keyup='someKeyUpFunction'

as:

@keyup='someKeyUpFunction'

Similarly, v-bind with :

v-bind:href='var1'

Can be written as:

:href='var1'

Hope it helps!

Solution 3 - vue.js

They may look a bit different from normal HTML, but : and @ are valid chars for attribute names and all Vue.js supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is totally optional, but you will likely appreciate it when you learn more about its usage later.

Source: official documentation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLorenzoBertiView Question on Stackoverflow
Solution 1 - vue.jsFitzFishView Answer on Stackoverflow
Solution 2 - vue.jsNitin KumarView Answer on Stackoverflow
Solution 3 - vue.jsdharmaView Answer on Stackoverflow