vue 组件内获取actions的response方式
时间:2021-06-18 08:43:59|栏目:vue|点击: 次
最近使用在学习使用vuex,想利用vuex集中管理状态。在和后台进行数据交互的时候,必然会涉及接口的调用,此类异步操作,通常写在action里面:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use('Vuex');
const actions = {
getComplete ({}) {
return new Promise((resolve, reject) => {
Vue.http.get('XXXXXX').then((response) => {
resolve(response);
}).catch((response) => {
reject(response);
});
});
}
}
export default new Vuex.Store({
actions
})
这里将接口的请求放置在promise中,利用promise异步的特性,可以在子组件中获取到接口调用成功后返回的参数:
export default {
......
created: function() {
this.$store.dispatch('getComplete').then(response => {
......
}).catch(response => {
......
})
}
}
除了这种方式,也可以使用mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store),具体使用方式详见:
上一篇:vue项目中企业微信使用js-sdk时config和agentConfig配置方式详解
栏 目:vue
下一篇:Vue使用vue-area-linkage实现地址三级联动效果的示例
本文标题:vue 组件内获取actions的response方式
本文地址:http://www.codeinn.net/misctech/143774.html






