无效的原因:await后不是一个promise,await后需要的是一个promise,这是因为它最主要的意图是用来等待 Promise 对象的状态被 resolved。
onMounted(async () => {
await A(); //如果A和B不是返回promise则await无效
await B();
C();
});
例如下面的写法就会导致onMounted中的await无效
const A = () => {
$axios.get(api, {}).then((res: any) => {});
};
const B = () => {
$axios.get(api, {}).then((res: any) => {});
};
为了使A和B返回promise,可以这么修改:
const A = async() => {
await $axios.get(api, {}).then((res: any) => {}); //这里的await会有效正是因为axios返回的是promise
};
const B = async() => {
await $axios.get(api, {}).then((res: any) => {});
};