Vue.js Vuex模块突变
我需要给vuex模块打电话,它不工作。我看过文档,但仍然不起作用。希望有人能帮助我Vue.js Vuex模块突变,vue.js,vuex,Vue.js,Vuex,我需要给vuex模块打电话,它不工作。我看过文档,但仍然不起作用。希望有人能帮助我 const stateLocations ={ state: { provinces: {}, cities: {} }, mutations: { provinces(state, payload){ state.provinces = payload } } } const sto
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('provides',response.data)
TY因为您没有在模块中启用
名称空间
,您只需要
this.$store.commit('provinces', response.data)
如果要启用命名空间,请执行以下操作:
const stateLocations = {
namespaced: true,
state: {
...
然后你像这样进行突变:
this.$store.commit('locations/provinces', response.data)
Offtop,但我应该注意,根据vuex文档,这种使用外部api的方式是不正确的。此类逻辑应移到vuex模块的
操作
部分。