vuex 存储订单类型的字段,如何让他在没有数据的时候再去请求接口?-灵析社区

一一在debug

const state = { types: [], } export default { state, mutations } A页面B页面C页面都会用到订单类型 store.getters.types ,但我并不想这三个页面都写接口请求数据。怎么把接口请求放在vuex中管理。比如store.getters.types调用的时候有数据,就返回数据。没有就接口请求到数据?

阅读量:191

点赞量:0

问AI
const orderMixin = { computed: { types() { return this.$store.state.types; } }, created() { this.$store.dispatch('updateTypes'); } } const store = new Vuex.Store({ state: { types: [] }, actions: { updateTypes (ctx) { if(!ctx.state.types.length) { // fetch } } } }) export default { name: 'A', mixins: [orderMixin], }