首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个调度后运行Vuex函数

多个调度后运行Vuex函数
EN

Stack Overflow用户
提问于 2019-02-19 05:02:30
回答 1查看 7.3K关注 0票数 8

我正在创建一个应用程序,在某一点上我需要加载一些数据,但是对于用户来说,为了不看到损坏的数据,我要插入一个加载组件。

目前,我加载了一个setTimeout,但是在某个时候,API响应可能需要超过1秒。因此,我只想在所有调度完成后才更新加载状态。

MainComponent.vue

代码语言:javascript
复制
beforeCreate() {
    this.$store.dispatch('responsibles/fetchData')
    this.$store.dispatch('events/fetchData')
    this.$store.dispatch('wallets/fetchData')

    // Need to run this setTimeout after all the above dispatches become completed...
    setTimeout(() => {
        this.loading = false
    }, 1000)
}

store/modules/responsibles.js

代码语言:javascript
复制
const state = {
    responsibles: []
}

const actions = {
    fetchData({dispatch}) {
        function getresponsibles() {
            return http.get('responsibles')
        }

        axios.all([
            getresponsibles()
        ]).then(axios.spread(function (responsibles) {
            dispatch('setResponsibles', responsibles.data.data)
        })).catch(error => console.error(error))
    },
    setResponsibles({commit}, responsibles) {
        commit('SET_RESPONSIBLES', responsibles)
    }
}

const mutations = {
    SET_RESPONSIBLES(state, responsibles) {
        state.responsibles = responsibles
    }
}

store/modules/events.js

代码语言:javascript
复制
const state = {
    events: []
}

const actions = {
    fetchData({dispatch}) {
        function getEvents() {
            return http.get('events')
        }

        axios.all([
            getEvents()
        ]).then(axios.spread(function (events) {
            dispatch('setEvents', events.data.data)
        })).catch(error => console.error(error))
    },
    setEvents({commit}, events) {
        commit('SET_EVENTS', events)
    }
}

const mutations = {
    SET_EVENTS(state, events) {
        state.events = events
    }
}

store/modules/wallets.js

代码语言:javascript
复制
const state = {
    wallets: []
}

const actions = {
    fetchData({dispatch}) {
        function getWallets() {
            return http.get('wallets')
        }

        axios.all([
            getWallets()
        ]).then(axios.spread(function (wallets) {
            dispatch('setWallets', wallets.data.data)
        })).catch(error => console.error(error))
    },
    setWallets({commit}, wallets) {
        commit('SET_WALLETS', wallets)
    }
}

const mutations = {
    SET_WALLETS(state, wallets) {
        state.wallets = wallets
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-19 05:22:30

  1. 让您的操作return Promise由Axios创建,如 返回axios.all(…… 请参阅https://vuex.vuejs.org/guide/actions.html#composing-actions
  2. 将您的分派调用包装在Promise.all中,等待它们全部完成 this.$store.dispatch('responsibles/fetchData'),(Promise.all this.$store.dispatch('events/fetchData'),this.$store.dispatch(‘wallet/fetchData’)).finally() => { //使用“最终”,因此即使有错误,也停止“加载”this.loading = false })
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54759153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档