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

鍍金池/ 問答/HTML/ vuex如何通過dispatch操作ajax提交表單數(shù)據(jù),然后拿到返回的狀態(tài)

vuex如何通過dispatch操作ajax提交表單數(shù)據(jù),然后拿到返回的狀態(tài)

最近做了一個項目,在提交表單的時候,把ajax方法寫在了vuex的actions中,但是,當我執(zhí)行如下代碼之后,發(fā)現(xiàn)彈出來的還是原來的值,如果設置一個定時器如1s之后再彈出,就可以了,請問有什么好的辦法解決嗎,我想拿到提交后的狀態(tài),但是,dispatch是異步的,無法及時拿到
this.$store.dispatch('sendServerLoanCustomFilledDatas', this.filledData)

    .then(() => {
      alert(this.filledData.status)
    })
回答
編輯回答
愛礙唉

其實有兩種方式可以實現(xiàn):

方法一:async/await

// actions.js
async sendServerLoanCustomFilledDatas({commit}) {
    awiat fetch(`/api/xxx`);
}

// template
async submit() {
    await this.$store.dispatch('sendServerLoanCustomFilledDatas', this.filledData);
    console.log(this.filledData.status);
}

方法二:組合Action

核心就是在你的action中返回一個promise
// actions.js
sendServerLoanCustomFilledDatas({commit}) {
    return new Promise((resolve, reject) => {
        fetch(`/api/xxx`)
        .then(res => res.json())
        .then(resData => {
            commit('xxxCommit', resData);
            resolve();  
        })
        .catch(reject);
    });
}

// template
this.$store.dispatch('sendServerLoanCustomFilledDatas', this.filledData)
.then(() => {});
2017年11月27日 02:54