我需要调用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
答案 0 :(得分:21)
由于您未在模块中启用namespaced
,因此您只需
this.$store.commit('provinces', response.data)
如果要启用命名空间:
const stateLocations = {
namespaced: true,
state: {
...
然后你提交这样的变异:
this.$store.commit('locations/provinces', response.data)