目前我有一个存储区,它有两个状态属性——version和champions+2个操作,它们发出GET请求,然后提交到状态.第二个GET请求URL需要包含我从第一个GET请求获得的版本,如下所示:
axios.get("https://ddragon.leagueoflegends.com/cdn/" + X + "/data/en_US/champion.json")
此代码中的X应该是Vuex state version属性.如果我想从Vuexstore 外访问该房产,我会这样做:
this.$store.state.version
不过,当我在store 试用时,它似乎不起作用.我应该如何从getChampions操作中访问version state属性?
代码:
export default new Vuex.Store({
state: {
version: null,
champions: null
},
mutations: {
version(state, data){
state.version = data.version
},
champions(state, data){
state.champions = data.champions
}
},
actions: {
getVersion({commit}){
axios.get("http://ddragon.leagueoflegends.com/api/versions.json")
.then((response) => {
commit('version', {
version: response.data[0]
})
})
.catch(function (error) {
console.log(error);
})
},
getChampions({commit}){
axios.get("https://ddragon.leagueoflegends.com/cdn/" + X + "/data/en_US/champion.json")
.then((response) => {
commit('champions', {
champions: response.data.data
})
})
.catch(function (error) {
console.log(error);
})
}
}
})