Vue 2 removes former event listeners
See original GitHub issueI’m currently trying VueJS 2 and noticed a change when using it on existing DOM Indeed everytime VueJS is initialised it will do something on existing DOM and will remove events previously binded outside of VueJS.
// We bind an event
document.querySelector('#demo').addEventListener('click', function () {
alert("i'm a button")
})
// and it's lost when Vue is binded
new Vue({
el: '#vuejs'
})
This same example works fine with VueJS1. I don’t have enough understanding of Vue core to understand what changed between the two version of the compiler.js
that would explain this change.
Issue Analytics
- State:
- Created 7 years ago
- Comments:25 (3 by maintainers)
Top Results From Across the Web
How to remove event listener from Vue Component
You can use this.$el for whole component and destroy event like you created it: Vue.component('Child', { template: ` <div class="child"> ...
Read more >$listeners removed - Vue 3 Migration Guide
In Vue 3's virtual DOM, event listeners are now just attributes, prefixed with on , and as such are part of the $attrs...
Read more >Vue: removing event listener on destroy : r/vuejs - Reddit
If you are able to "see" your event in the desired component, then you should be able to remove it with this.$off on...
Read more >Introduction to events - Learn web development | MDN
Removing listeners. If you've added an event handler using addEventListener() , you can remove it again using the removeEventListener() method.
Read more >How to Clean Up after Yourself Why It's Important Vue React
In Vue we can listen for the hook:beforeDestroy event on component instance, and pass a callback which will remove the event listener. created() ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
what if you’re in the process of trying to migrate to vue, but you have some legacy parts in jquery trying to move to vue now means an all or nothing approach this bit me trying out my first vue component amongst my legacy components
so what you’re saying is that I have to move all of them to vuejs and there is no way to intermingle?
I’m new to Vue.js and I stumbled upon this issue in a project where we want to migrate from jquery to Vue incrementally. After a week of try and error I finally found this issue on Github. So I put the Vue code above the old jquery code and finally everything works. 🎉
This behavior should be definitely in the docs, maybe somewhere at the beginning.