Vuex模块突变

Rbe*_*bex 7 vue.js vuex

我需要调用vuex模块它不起作用.我已经看过文档,但它仍然不起作用.希望可以有人帮帮我.

const stateLocations ={

    state: {

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

            state.provinces = payload
        }
    }
}


const store = new Vuex.Store({

    modules: {
        locations: stateLocations
    }


})
Run Code Online (Sandbox Code Playgroud)

我的代码称为突变

created(){

            var store = this.$store

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

                    store.state.locations.commit('provinces', response.data)
                })
                .catch()
        }
Run Code Online (Sandbox Code Playgroud)

这个不适用store.state.locations.commit('provinces', response.data) TY

Cod*_*Cat 32

因为您没有namespaced在模块中启用,所以您只需要

this.$store.commit('provinces', response.data)
Run Code Online (Sandbox Code Playgroud)

如果要启用命名空间:

const stateLocations = {
  namespaced: true,
  state: {
    ...
Run Code Online (Sandbox Code Playgroud)

然后你像这样提交变异:

this.$store.commit('locations/provinces', response.data)
Run Code Online (Sandbox Code Playgroud)