Vuex Modules Couplings

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 for calling mutations

created(){

            var store = this.$store

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

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

      

It doesn't work store.state.locations.commit('provinces', response.data)

TY

+3


source to share


1 answer


Since you haven't included namespaced

in the module, you just need

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

      

If you want to include the namespace:



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

      

then you make the mutation like this:

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

      

+9


source







All Articles