cust_down 定时器小于1 就调用clear的方法 如何在内部调用啊 我尝试了各种写法都报错 还是说vuex根本不提倡这种写法啊
mutations只能通过dispatch来调用...
把定时器放在action中, mutations只改变数据
在action
里做吧,类似:
action
let timerId;
export const countdown = function({dispatch, state}) {
timerId = setInterval(function() {
dispatch('COUNT_DOWN');
if(state.count < 1){
clearInterval(timerId);
}
}, 1000);
};
mutation
const mutations = {
['COUNT_DOWN'](state) {
state.count--;
}
};
把 ['STOP'](state)
改成 this.STOP(state)
试试,
let obj = {
['hello']() {
console.log('hello world');
},
['add'](x) {
console.log(x+1);
this.hello();
}
}
// obj.hello();
obj.add(1);
以上代码可以正确运行的