Pablo Gablo
Pablo Gablo

Reputation: 55

vuex: why calling mutation from within a namespaced module requires a prefix?

I have the following vuex config

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

    },

and the listing module code looks like

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

when calling this.commit('updateListingAllItems', response.data) I'm getting [vuex] unknown mutation type: updateListingAllItems.

the vuex guide says

Namespaced getters and actions will receive localized getters, dispatch and commit. In other words, you can use the module assets without writing prefix in the same module

Why am I getting the error message then ?

Upvotes: 2

Views: 1277

Answers (1)

psalaets
psalaets

Reputation: 717

Inside an action method this is the store. Your code commits an non-prefixed mutation on the store instance.

If you change

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

to

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

you'll get what you expect.

Upvotes: 2

Related Questions