1
我试图让使用Vue公司+ Vuex一个游戏,但在该项目中,我意识到了游戏的互动性最强的部分,必须作出Phaser,然后我决定使用Phaser来保存UI的Vue + Vuex。失去“这个”范围在Vue公司+ Vuex +移相无功通话
一切都会比预期的,直到我试图从Vuex获得移相器的第一个数据越好,问题是,我失去了当前对象的范围。
我使用store.watch();
得到改变,但是当我尝试调用其他功能我得到一个错误。
import store from '../vuex/store';
import Creature from './Creature';
const creatures = [];
export default {
create() {
store.watch(this._newCreatureGetter, this._newCreatureRX);
for (const creature of this._creaturesGetter()) {
this._addCreature(creature);
}
},
_addCreature(creature) {
creatures.push(new Creature(this.game, creature));
},
_newCreatureGetter() {
return store.state.newCreature;
},
_newCreatureRX(newCreature) {
console.log(this);
this._addCreature(newCreature);
},
};
在从移相器的_newCreatureRX
这个码是VueX调用,当我尝试打电话_addCreature
我得到一个错误,告诉_addCreature
没有定义。 console.log(this)
显示一个Vue对象。
我如何得到这个正常工作?我必须以不同的方式构建我的项目吗?
这工作!谢谢!我不知道我可以用.bind(this)发送回调。它现在非常有意义! – Drico
很容易错过。问题是因为在Vue.js中,它通过'cb.call(vm,watcher.value)'执行回调,其中'vm'是'Vue'实例。请参阅https://github.com/vuejs/vue/blob/769c4dc2032251323c8f61ad8eba2c26c615a618/src/core/instance/state.js#L228 – Phil