Vuex - Run function after multiple dispatches

纵饮孤独 提交于 2020-04-12 19:54:30

问题


I'm creating an application and at a certain point I need to load some data, but for the user not to see broken data I'm inserting a loading component.

Currently I put a setTimeout in the load, but at some point the API response can take more than 1 second. So I would like to update the loading state only when all the dispatches become completed.

MainComponent.vue

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

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

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

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
    }
}

回答1:


  1. Have your actions return the Promise created by Axios, eg

    return axios.all(...
    

    See https://vuex.vuejs.org/guide/actions.html#composing-actions

  2. Wrap your dispatch calls in Promise.all and wait for them all to complete

    Promise.all([
      this.$store.dispatch('responsibles/fetchData'),
      this.$store.dispatch('events/fetchData'),
      this.$store.dispatch('wallets/fetchData')
    ]).finally(() => {
      // using "finally" so even if there are errors, it stops "loading"
      this.loading = false
    })
    


来源:https://stackoverflow.com/questions/54759153/vuex-run-function-after-multiple-dispatches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!