Why VueJs introduce .native Event modifier ? What

2020-05-23 01:08发布

问题:

I din't get any docs for native event modifiers. I have seen some where like this:

<router-link @click.native="pressThis()"> Press here </router-link>

what is the use of native modifier on router-link click event. and what other use case native modifier can have ?

回答1:

You can override Vue events in custom components. For instance, you might have a list component that once you click an item you call this.$emit('click', selectedItemData), and that will emit the click event to the parent component that is watching that.

However, sometimes you really want to bind to the native HTML/DOM event listener element.addEventListener('click', callThisMethod), and that's the use of .native. Also, make a note that it will handle cleaning the event listener once your component gets destroyed just like a non-native event.

In sum: use .native when you need the 'raw' event from DOM.



标签: vue.js vuejs2