【发布时间】:2020-04-13 02:55:16
【问题描述】:
我正在尝试获取使用 axios 调用 vuex 操作的响应对象,我需要对象中的响应,但由于某种原因它说未定义。
在我的组件方法中:
mounted()
{
console.log(this.$options.name+' component successfully mounted');
this.$store.dispatch(this.module+'/'+this.action, this.payload)
.then((response) =>
{
console.log(response);
this.items = response.data.data;
this.pagination = response.data.pagination;
});
},
我的 vuex 操作:
list({ commit }, payload)
{
commit( 'Loader/SET_LOADER', { status:1 }, { root: true });
return axios.get('/api/posts', { params: payload })
.then((response) => {
commit('Loader/SET_LOADER', { status:2, response:response }, { root: true });
console.log(response);
commit('SET_POSTS', response.data.data.data );
commit('SET_PAGINATION', response.data.pagination );
})
.catch((error) => {
commit('Loader/SET_LOADER', { status:3, errors: error }, { root: true });
throw error;
});
},
使用 vuex 状态不是一个选项,我想使用组件数据属性项和分页来代替。
【问题讨论】:
-
原因是你没有返回值,所以它是未定义的。从动作返回值并不常见,因为它应该在存储中设置值,这就是它的作用。您可以添加
return response或从 this.$store 读取值。
标签: javascript vue.js