Vuex 模块变更

Vuex Modules Mutations

我需要调用 vuex 模块它不起作用。我看过文档,但仍然无法正常工作。希望有人能帮助我。

const stateLocations ={

    state: {

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

            state.provinces = payload
        }
    }
}


const store = new Vuex.Store({

    modules: {
        locations: stateLocations
    }


})

我调用突变的代码

created(){

            var store = this.$store

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

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

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

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

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

如果要启用命名空间:

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

然后你像这样提交突变:

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