【问题标题】:async/await actions in VuexVuex 中的异步/等待操作
【发布时间】:2018-06-03 15:20:21
【问题描述】:

我想知道如何在 Vuex 中使用异步/等待操作。 docs 提供此语法作为示例:

actions: {
  async actionA ({ commit }) {
    commit('gotData', await getData())
  },
  async actionB ({ dispatch, commit }) {
    await dispatch('actionA') // wait for `actionA` to finish
    commit('gotOtherData', await getOtherData())
  }
}

按照这个例子,我有:

import Vue from 'vue';
import Vuex from 'vuex';
import * as firebase from 'firebase';

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        // other state vars here
        resource: null
    },
    mutations: {
        // saveValues
        setResource(state, payload) {
            state.resource = payload;
        }
    },
    actions: {
        async getResource({ commit, dispatch }) {
            var resource
            console.log('resource1: ' + resource)
            Vue.http.get('https://mysite/api/getResource')
                .then((response) => {
                    console.log('get resource')
                    var data = response.body;
                    resource = data.access_resource;
                    console.log('resource2: '+ resource)
                    commit('setResource', resource);
                    var foo = store.getters.resource;
                    console.log('resource3: ' + foo);
                }, (error) => {
                    console.log(error);
                });
        },
        async getSomeApi({ commit, dispatch }) {
            console.log('getting api');
            await dispatch('getResource');
            var resource = store.getters.resource;
            console.log('resource4: ' + resource);
            Vue.http.get('https://somesite/api/someapi?resource=' + resource)
                .then((response) => {
                    console.log("got something from somesite")
                    var data = response.body;
                    // do something with data -> payload
                    dispatch('saveValues', payload);
                }, (error) => {
                    console.log(error);
                });
        }
    },
    getters: {
        resource(state) {
            return state.resource;
        }
    }
});

但是,即使遵循文档中的语法示例,当我运行此代码时,async/await 似乎也被完全忽略了。当我查看日志时,我看到的顺序如下:

  • getting api
  • resource1: undefined
  • resource4: null
  • get resource
  • resource2: <expected-value>
  • resource3: <expected-value>

我希望 console.log 语句以数字顺序打印出来。如果有人能澄清我做错了什么,我将不胜感激。

【问题讨论】:

    标签: vue.js async-await vuex


    【解决方案1】:

    你不是 awaitVue.http.get()getResource() 方法中的承诺,所以 await dispatch('getResource') 将在 HTTP 请求解决之前解决。

    修剪:

    async getResource() {
        let response
    
        try {
            response = await Vue.http.get('https://mysite/api/getResource')
        } catch (ex) {
            // Handle error
            return
        }
    
        // Handle success
        const data = response.body
    }
    

    【讨论】:

    • 来自您的回答“等待调度('getResource')将在 HTTP 请求解决之前解决。”这是否意味着异步过程将比同步过程更快完成?
    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多