我需要在created(). 为此,我需要制作created()as async。根据 Vue 文档,created()称为synchronously。Vue 框架会await避免created()任何竞争条件吗?
this.isAuthenticated = await authService.isAuthenticated();
Run Code Online (Sandbox Code Playgroud)
Vue.config.productionTip = false;
function tm(ms, msg) {
return new Promise(resolve => {
setTimeout(() => {
resolve(msg);
}, ms);
});
}
new Vue({
async beforeCreate() {
console.log(await tm(1000, "BEFORE CREATE"));
},
async created() {
console.log(await tm(2000, "CREATED"));
},
async beforeMount() {
console.log(await tm(3000, "BEFORE MOUNT"));
},
async mounted() {
console.log(await tm(4000, "MOUNTED"));
}
}).$mount("#app");Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18678 次 |
| 最近记录: |