[英]Vuex mutation not working synchronously but neither returning a promise
My vuex mutation seems to not work synchronous.我的 vuex 突变似乎无法同步工作。 It looks like this:它看起来像这样:
mutations: {
computeStatusData(state, status) {
if (status.active !== true) { return }
const started = new Date(status.startedAt);
started.setHours(0, 0, 0, 0);
const today = new Date();
status.currentDay = Math.floor((today.getTime() - started.getTime()) / (1000 * 60 * 60 * 24));
status.currentWeek = Math.floor(status.currentDay / 7);
if (!status.programm.sections) { console.log('No Sections'); return; }
for (let i = 0; i < status.programm.sections.length; i++) {
if (status.currentWeek <= status.programm.sections[i].to) {
status.currentSection = i;
console.log('Current Section', status.currentSection)
break;
}
}
console.log('new status!', status)
},
In a method of my component I call it like this:在我的组件的方法中,我这样称呼它:
async loadSections() {
await somethingElse();
if (this.status && this.programm.sections) {
console.log('Recomputing Status')
this.status.progamm = this.programm;
this.$store.commit('computeStatusData', status);
console.log('updated Status', this.status)
}
this.$nextTick(() => { this.$forceUpdate(); })
},
My console log looks like this:我的控制台日志如下所示:
Recomputing Status
updated Status {…}
Current Section 0
new status! {…}
If the mutation was synchronous it should log Current Section 0
and new Status!
如果突变是同步的,它应该记录Current Section 0
和new Status!
before updated Status
but it doesn't.在updated Status
之前,但它没有。
It doesnt seem to return a Promise because when I do console.log(this.$store.commit('computeStatusData', status));
它似乎没有返回 Promise,因为当我执行console.log(this.$store.commit('computeStatusData', status));
it just logs undefined
.它只是记录undefined
。
The correct way of mutate a status is inside an action.改变状态的正确方法是在动作中。
https://vuex.vuejs.org/guide/actions.html https://vuex.vuejs.org/guide/actions.html
A helpful question: Vuex Action vs Mutations一个有用的问题: Vuex Action vs Mutations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.