Vuex 模块突变



我需要调用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)

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

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

如果要启用命名空间:

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

然后你像这样提交突变:

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

最新更新