在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vuex中使用dispatch().then()沒有等待dispatch完成

vuex中使用dispatch().then()沒有等待dispatch完成

組件中代碼如下

    beforeRouteEnter(to, from, next) {
        log.debug('beforeRouteEnter');
        next(vm => {
            vm.$store.dispatch('getCategorys').then(() => {
                log.debug(JSON.stringify(vm.$store.state.categorys));
            });
        });
    },

Actions中的代碼如下

    getCategorys(context) {
        Vue.http.get(url.categorysList).then((response) => {
            let result = response.data;
            if (result != null) {
                log.debug('updateStateCategorys');
                context.commit('updateStateCategorys', result);
            }
        });
    }
 DBG  page/menu/menu  22:01:43:353 - null
 DBG  vuex/actions  22:01:43:381 - updateStateCategorys

現(xiàn)在執(zhí)行的時(shí)候發(fā)現(xiàn),代碼不是打印log(updateStateCategorys)后打印出得到的結(jié)果,而是直接打印null(未獲取到數(shù)據(jù)).
這個(gè)是怎么回事呢?大神幫忙

回答
編輯回答
壞脾滊

把Promise return出去。

getCategorys(context) {
    return Vue.http.get(url.categorysList).then((response) => {
        ...
    });
}
2018年3月17日 15:26