我是 vue js 的新手,我有一種情況,我必須在 getUser api 呼叫完成后執行函式 getUserViews()。但看起來 getUserViews 在 api 呼叫完全完成之前正在執行。
async initUserLogin() {
await this.getAccountNumber();
if (!this.userHasDetails) {
this.getUser();
}
},
mounted() {
this.$store.commit(types.user.setLogIn);
this.watchBreakpoints();
this.getUserViews();
if (this.loginUser) {
this.initUserLogin();
}
},
uj5u.com熱心網友回復:
您必須在之前添加 await this.getUser()(如果這回傳一個承諾)
嘗試在異步呼叫之后添加 get 函式。然后使用.then(),當請求成功時會呼叫這個。
有關異步/等待處理的更多資訊可以在這里找到
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
async initUserLogin() {
await this.getAccountNumber();
if (!this.userHasDetails) {
await this.getUser(); //if this returns a promise
}
},
mounted() {
this.$store.commit(types.user.setLogIn);
this.watchBreakpoints();
if (this.loginUser) {
this.initUserLogin().then((response) => {
this.getUserViews();
})
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/418951.html
標籤:
上一篇:有沒有辦法防止vuehotreload進行過多的API呼叫?
下一篇:如何在表單中分支?
