Vuex突变不能同步工作,但都没有返回承诺

问题描述 投票:0回答:1

我的vuex突变似乎不能同步。它看起来像这样:

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)
    },

在我的组件的方法中,我称之为:

    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(); }) 
    },

我的控制台日志如下所示:

Recomputing Status
updated Status {…}
Current Section 0
new status! {…}

如果突变是同步的,它应该在Current Section 0之前记录new Status!updated Status,但事实并非如此。

它似乎没有返回一个承诺,因为当我做console.log(this.$store.commit('computeStatusData', status));它只记录undefined

javascript vue.js vuex
1个回答
0
投票

改变状态的正确方法是在动作中。

https://vuex.vuejs.org/guide/actions.html

一个有用的问题:Vuex Action vs Mutations

© www.soinside.com 2019 - 2024. All rights reserved.