admin管理员组文章数量:1426484
I use Vue and have a method in load.js
.
async function loadTask(x) {
return await x; // Some async code
}
export { loadTask };
In a Vue ponent I call it but the await
prevent the <template>
to load. Without the await
, the code below runs. What do I miss?
<script setup>
import { loadTask } from './methods/load.js';
const test = loadTask(3);
console.log(await test);
</script>
<template>
<div>Does not run</div>
</template>
I use Vue and have a method in load.js
.
async function loadTask(x) {
return await x; // Some async code
}
export { loadTask };
In a Vue ponent I call it but the await
prevent the <template>
to load. Without the await
, the code below runs. What do I miss?
<script setup>
import { loadTask } from './methods/load.js';
const test = loadTask(3);
console.log(await test);
</script>
<template>
<div>Does not run</div>
</template>
Share
Improve this question
asked Nov 9, 2021 at 18:49
Jens TörnellJens Törnell
24.9k46 gold badges130 silver badges223 bronze badges
6
-
1
await
is not valid at the top level unless your environment supports it. I don't think a<script>
tag counts for top-level await, though. Are you not getting an error saying something along those lines? – VLAZ Commented Nov 9, 2021 at 18:51 -
@VLAZ - It would if it were
<script type="module">
but the above isn't (unless something does something to it before the browser sees it, thesetup
thing on it makes me wonder since I don't do Vue.js). – T.J. Crowder Commented Nov 9, 2021 at 18:54 -
@VLAZ No, I'm not getting any errors. One strange thing, if I write console.log('whatever') before
</script>
it runs, but i stops the output in the<template></template>
. – Jens Törnell Commented Nov 9, 2021 at 18:54 -
1
As @VLAZ said, most likely you're getting an error trying to use
await
at the top level outside a module. Perhaps the error is being obscured by a bundler or your environment? – T.J. Crowder Commented Nov 9, 2021 at 18:54 - 3 You can use top level await in Vue, but it must be used together with Suspense: 1. v3.vuejs/api/sfc-script-setup.html#top-level-await 2. stackoverflow./questions/69183835/… – Cianekjr Commented Nov 9, 2021 at 18:58
2 Answers
Reset to default 4According to the documentation:
async setup() must be used in bination with Suspense, which is currently still an experimental feature. We plan to finalize and document it in a future release - but if you are curious now, you can refer to its tests (opens new window)to see how it works.
https://v3.vuejs/api/sfc-script-setup.html#top-level-await
https://v3-migration.vuejs/breaking-changes/suspense.html#suspense
Vue <script setup> Top level await causing template not to render
So if you have async setup you have to import this ponent in parent like that:
import { defineAsyncComponent } from "vue";
export default {
ponents: {
HelloWorld: defineAsyncComponent(() =>
import("./ponents/HelloWorld.vue")
),
},
};
</script>
And use it in template wrapped in suspense:
<template>
<suspense>
<template #default>
<HelloWorld />
</template>
<template #fallback>
<div>Loading...</div>
</template>
</suspense>
</template>
Demo:
https://codesandbox.io/s/reverent-sinoussi-9r5or?file=/src/ponents/HelloWorld.vue
As described in Vue <script setup> Top level await causing template not to render,
in case you don't need the full power of async ponents and Suspense
, you can use either onBeforeMount
:
const test = ref('');
onBeforeMount(async () => {
test.value = await loadTask(3);
console.log(test.value);
})
or do the initialization inside an async
function that you invoke without await
ing for the promise to plete:
const test = ref('');
const init = async () => {
test.value = await loadTask(3);
console.log(test.value);
}
init();
or use promise chainig:
const test = ref('');
loadTask(3).then(task => {
test.value = task;
console.log(test.value);
});
You additionally need to take care of error handling as appropriate for each case.
本文标签: javascriptVue export import async await stopsStack Overflow
版权声明:本文标题:javascript - Vue export import async await stops - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745369066a2655653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论