[Vuejs]-VueJS weird $emit behaviour

0๐Ÿ‘

โœ…

I found the problem. I changed the mounted hook to:

mounted: function() {
        let _this = this;
        $(`#${this.randomID}`).on('hide.bs.modal', function(e) {
            _this.$emit('modal-close')
        })
    }

It was probably a scope problem. which has been solved by:

  1. Declaring the "instance copy" (_this) with let.
  2. Changing the callback function from the JQuery listener declaration from arrow function to a normal function.

It still feels quite strange because the event was being emitted, but that was probably because I also had an instance = this on my bootstrap-table component which was being globally scoped (รŒ was not using let either).

Leave a comment