我用Vue Dragula来拖拽。当我放弃时,它会触发方法:

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

}

现在this.championship是一个计算属性:
computed: {
    championship(){
        return this.championships.find((elem) => elem.championship == this.championship_id);
    },
}

其中championshipschampionship_id是全局数据。
并且console.log(this.championship);返回undefined
现在,我简化了,我写道:
computed: {
    championship(){
        return 2;
    },
}

继续返回
我的代码怎么了???

最佳答案

this在drop event函数中不再引用Vue实例。在调用this之前,尝试在mounted函数中设置对$nextTick的引用:

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

10-08 04:41