Vuex state boolen 属性 显示未定义

Vuex state boolen property shows undefined

我有一个布尔状态 属性 但它显示未定义?

export default {
    state: {
        userStateChanged: false,
...

在获取数据时,我在此状态 属性 上执行了 console.log 并且控制台显示未定义

        async FETCH_ALL_USERS(context, page) {

            console.log('state changed?', this.state.userStateChanged);

            if (this.state.userStateChanged === false) {
                return this.state.users;
            } else {
                console.log('return new set');
                const user  = await axios.get("/api/user?page=" + page);
                context.commit('SET_ALL_USERS', user.data);
            }
        },

在访问状态之前,您首先必须访问商店。 所以而不是 console.log('state changed?', this.state.userStateChanged); console.log('state changed?', this.$store.state.userStateChanged);

您应该通过 context 访问商店状态。

async FETCH_ALL_USERS(context, page) {

            console.log('state changed?', context.state.userStateChanged);

            if (context.state.userStateChanged === false) {
                return context.state.users;
            } else {
                console.log('return new set');
                const user  = await axios.get("/api/user?page=" + page);
                context.commit('SET_ALL_USERS', user.data);
            }
        },