【发布时间】:2020-05-04 07:49:51
【问题描述】:
我有一个 Vue 组件,我在其中执行了一堆异步调用,然后应该填充我的模板。但是,似乎没有任何更新,即使我可以在控制台中看到我的数据正在返回。我对 Vue 很陌生,所以我一定遗漏了一些东西。这是我的代码的简化版本:
<template>
<div id="my_component">
<div v-bind:class="{ hidden: !isLoading }">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin:auto;background:#fff;display:block;" width="200px" height="200px" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
<circle cx="50" cy="50" fill="none" stroke="#1ba4de" stroke-width="4" r="15" stroke-dasharray="70.68583470577033 25.561944901923447">
<animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="0.9803921568627451s" values="0 50 50;360 50 50" keyTimes="0;1"></animateTransform>
</circle>
</svg>
</div>
<div v-bind:class="{ hidden: isLoading }">
<h1>{{ result.title }}</h1>
</div>
</div>
</template>
<script>
export default {
name: 'MyComponent',
data () {
return {
isLoading: true,
result: null
}
},
async created () {
this.result = await myLongRunningAsyncFunction();
this.isLoading = true;
console.log(this.result) // Logs result of myLongRunningAsyncFunction()
}
methods: {
myLongRunningAsyncFunction: async function() {
// Make a bunch of API calls and get some data
}
}
}
</script>
【问题讨论】:
-
不应该是
created钩子中的this.isLoading = false;吗?
标签: javascript vue.js async-await