首页 文章

vuex:为什么在命名空间模块中调用变异需要前缀?

提问于
浏览
0

我有以下vuex配置

import listing from '@/store/modules/listing'
var store = new Vuex.Store({
    modules:
    {
        listing: listing,

    },

和列出模块代码看起来像

import Vue from 'vue'
const listing = {
    namespaced: true,
    state: {
        listingAllItems: [],
        listingSelectedItems: [],       
    },
    mutations: {
        updateListingAllItems(state, param) {
        },
    },
    actions: {
        getListingItems(context, param) {
            var tempThis = this;
            return new Promise((resolve, reject) => {
                var url = 'http://WS/';
                Vue.http.get(url).then(response => {
                    tempThis.commit('updateListingAllItems', response.data);
                }).catch(error => reject(error));
            })
        },
    },
    getters: {}
}

export default listing

在致电 this.commit('updateListingAllItems', response.data) 时我正在 [vuex] unknown mutation type: updateListingAllItems .

vuex guide

Namespaced getter和actions将接收本地化的getter,dispatch和commit . 换句话说,您可以使用模块资产而无需在同一模块中写入前缀

为什么我收到错误消息呢?

1 回答

  • 0

    在一个动作方法 this 里面是商店 . 您的代码在商店实例上提交了一个非前缀的变异 .

    如果你改变了

    tempThis.commit('updateListingAllItems', response.data);

    context.commit('updateListingAllItems', response.data);

    你会得到你期望的 .

相关问题