[Vuejs]-Vue js listen default events with vm.$on

0👍

You need to emit an event you want to listen to first, use vm.$emit() like this:

let vm = new Vue({
  el:"#app",
  methods:{
    clickListener() {
      this.$emit('eventName', 'sample-value');
    }
  }
})

vm.$on('eventName', function(value){
  console.log(value); // 'sample-value'
});

EDIT: try something like this:

const vm = new Vue({
  el: '#app',
  mounted() {
    const app = document.getElementById('app');
    app.addEventListener('click', (event) => {
      this.$emit('click', event);
    });
  }
});

vm.$on('click', (event) => {
  console.log('click', event);
});

Leave a comment