1

I use Vue-Dragula to drap and drop. When I drop, it triggers the method:

mounted: function () {
    this.$nextTick(function () {
        Vue.vueDragula.eventBus.$on(
            'drop',
            function (args) {
                console.log(this.championship);
            }
        );

}

Now this.championship is a computed property:

computed: {
    championship(){
        return this.championships.find((elem) => elem.championship == this.championship_id);
    },
}

where championships and championship_id are global datas.

and console.log(this.championship); returns undefined

Now, I simplify, and I write:

computed: {
    championship(){
        return 2;
    },
}

and console.log(this.championship); keep returning undefined

What is wrong with my code???

1

2 Answers 2

3

Use arrow functions so your this will be the actual Vue instance:

mounted () {
    this.$nextTick(() => {
        Vue.vueDragula.eventBus.$on(
            'drop',
            args => console.log(this.championship)
        );
    })
}
Sign up to request clarification or add additional context in comments.

Comments

2

this isn't referring to the Vue instance anymore in the drop event function. Try setting a reference to this in the mounted function before the $nextTick call:

mounted: function () {
  let vm = this;
  this.$nextTick(function () {
    Vue.vueDragula.eventBus.$on(
      'drop',
      function (args) {
        console.log(vm.championship);
      }
    );
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.