iT邦幫忙

0

Vuex actions中如何使用async/await dispatch

####Vuex actions中如何使用async/await dispatch

signUp ({commit, dispatch}) {
    axios.post(authUrl).then(res => {
        ...
        dispatch('storeAndFetch', data)
    })
},

async storeAndFetch ({commit, dispatch}, data) {
    await dispatch('store', data)
    dispatch('fetch', data)
}

store ({commit}, data) {
    return new Promise((resolve, reject) => {
        axios.post(databaseUrl, data).then(res => {
            resolve()
        })
    })
}

fetch ({commit}, data) {
    axios.get(databaseUrl).then(res => {
        ...
        commit()
    })
}

練習做登入系統,註冊成功後,先把資料傳到資料庫,再從資料庫比對email取出使用者資料
上面程式碼執行時有成功store但fetch並沒有被執行

想請問使用上錯誤在哪或是有其他更好的方式

action是非同步的commit
我不是很確定action中使用dispatch是不是可以這樣使用
這樣action dispatch action我覺得其實有點鳥呵呵
你的每次dispatch應該會很亂
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
dragonH
iT邦超人 5 級 ‧ 2019-09-24 16:11:02

codepen

就這樣

你的問題可能要貼比較完整的 code

我要發表回答

立即登入回答