๏ผPromiseใปasync/await
ใๅซใ Composition API ใณใณใใผใใณใใฎใในใ๏ผ
await
ใๅฟใใใจใในใใๆญฃใใๅใใชใawait nextTick()
ใๅฟ
่ฆโ Vuex ใฎ action ใๅ
้จใฎ async
้ขๆฐใใในใใใใจใใฏๅฟ
้ ็ฅ่ญใ
dispatch
ใๅผใถ Composition API ใณใณใใผใใณใใ<!-- AsyncCounter.vue -->
<template>
<button @click="incrementAsync">+</button>
</template>
<script lang="ts" setup>
import { useStore } from 'vuex';
const store = useStore();
const incrementAsync = async () => {
await store.dispatch('incrementAsync'); // โ ้ๅๆ action
};
</script>
// store/counter/actions.ts
export const actions = {
async incrementAsync({ commit }: any) {
await new Promise((resolve) => setTimeout(resolve, 500)); // 500ms ๅพ
ใค
commit('increment');
},
};
dispatch
ใใขใใฏใใฆ Promise ใ่ฟใawait
ใใคใใฆใในใใๆญฃใใๅพ
ใคnextTick
ใไฝฟใ