我用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);
},
}
其中
championships
和championship_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);
}
);
}
}