【发布时间】:2018-01-29 19:44:01
【问题描述】:
我对 vue 很陌生(对 vuex 也很陌生)。我想将一些 axios api 调用移动为我的 Vuex 商店中的操作。我知道例如:
actions:{
LOAD_USER: function ({ commit }) {
axios.get('/arc/api/v1/me', {dataType: 'json'})
.then((response )=> {
commit('SET_USER', { user: response.data.user })
})
.catch(function (error) {
console.log(error.message);
});
并通过以下方式在我的调用组件中调用它:
this.$store.dispatch('LOAD_USER')
这是有效的。我的问题是我需要将调用组件中的一些变量设置为 false 或终止进度条。这是我之前在调用组件中使用的内容:
this.loading = true
this.$Progress.start()
axios.get('/arc/api/v1/me', {dataType: 'json'})
.then((response )=> {
this.$Progress.finish()
this.loading = false
this.$store.state.user = response.data.user;
this.user = this.$store.state.user
})
.catch(function (error) {
this.$Progress.fail()
console.log(error.message);
});
如何将这些加载行为集成到我的 vuex 操作中?我将如何通过此调用传递对我的组件的引用:
this.$store.dispatch('LOAD_USER')
或者有更好的解决方案吗?
【问题讨论】: