Home >Web Front-end >Vue.js >EventEmit removes the defined method in vue
Two methods to remove event listeners in Vue: use the v-on directive: first add the listener, and then use the v-on or @ directive to set its value to null to remove it. Use the $off() method: remove the listener directly from the Vue instance, specifying the event name and handler function.
Two methods to remove event listeners in Vue
Method 1: Use v-on
Command
Use the v-on
command to dynamically add or remove event listeners.
<code class="html"><button v-on:click="handleClick">按钮</button></code>
To remove a listener, use the v-on
shorthand form @
and set its value to null
.
<code class="html"><button @click="null">按钮</button></code>
Method 2: Use $off()
method
$off()
method can be used from the Vue instance Remove event listener.
<code class="js">this.$off('click', this.handleClick);</code>
Among them, 'click'
is the event name, this.handleClick
is the event handling function.
How to choose the removal method
v-on
Instructions are more convenient. $off()
method. The above is the detailed content of EventEmit removes the defined method in vue. For more information, please follow other related articles on the PHP Chinese website!