我需要调用下面的方法created()
。为此,我需要做created()
as async
。根据 Vue 文档,created()
叫做同步地。将Vue框架await
on created()
避免任何竞争条件?
this.isAuthenticated = await authService.isAuthenticated();
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");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)