question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue 2 removes former event listeners

See original GitHub issue

I’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'
})

JSFiddle

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:closed
  • Created 7 years ago
  • Comments:25 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
oppianmattcommented, Apr 10, 2017

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?

4reactions
Art4commented, Dec 21, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found