Vuex-多次调度后运行功能

Cai*_*aki 6 javascript vue.js vuex vue-cli-3

我正在创建一个应用程序,并在某个时候需要加载一些数据,但是为了让用户看不到损坏的数据,我正在插入一个加载组件。

目前,我将setTimeout放入了负载中,但是在某些时候,API响应可能会花费超过1秒的时间。因此,我只想在所有调度完成后才更新加载状态。

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)
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*hil 7

  1. 有你的行动returnPromise通过Axios公司,如创建

    return axios.all(...
    
    Run Code Online (Sandbox Code Playgroud)

    参见https://vuex.vuejs.org/guide/actions.html#composed-actions

  2. 将调度呼叫打包,Promise.all然后等待它们全部完成

    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
    })
    
    Run Code Online (Sandbox Code Playgroud)