Rbex
Rbex

Reputation: 1539

Vuex Modules Mutations

I need to call vuex module it doesn't work. I've seen the documentation but still it doesn't work. Hope someone can help me.

const stateLocations ={

    state: {

        provinces: {},
        cities: {}
    },
    mutations: {
        provinces(state, payload){

            state.provinces = payload
        }
    }
}


const store = new Vuex.Store({

    modules: {
        locations: stateLocations
    }


})

My code to call the mutations

created(){

            var store = this.$store

            axios.get('api/provinces')
                .then( function(response){

                    store.state.locations.commit('provinces', response.data)
                })
                .catch()
        }

This one doesn't work store.state.locations.commit('provinces', response.data) TY

Upvotes: 16

Views: 23755

Answers (1)

CodinCat
CodinCat

Reputation: 15914

Because you didn't enable namespaced in the module, you simply need to

this.$store.commit('provinces', response.data)

If you want to enable namespace:

const stateLocations = {
  namespaced: true,
  state: {
    ...

then you commit the mutation like this:

this.$store.commit('locations/provinces', response.data)

Upvotes: 54

Related Questions