admin管理员组文章数量:1277896
Using Vue3, I'd like to do something like:
<p>Hi {{ getNameAsync() }}</p>
However this is rendering as
<p>Hi "[object Promise]"</p>
Is there a solution?
Using Vue3, I'd like to do something like:
<p>Hi {{ getNameAsync() }}</p>
However this is rendering as
<p>Hi "[object Promise]"</p>
Is there a solution?
Share Improve this question asked Feb 24 at 2:06 Steve BennettSteve Bennett 127k45 gold badges185 silver badges243 bronze badges1 Answer
Reset to default 1The result returned by async
is of type Promise<>
, so {{ getNameAsync() }}
will display in the page [Object Promise]
, to display the correct content, you need to use ref
to store the content and use it in the page.
NOTICE: you can't use {{ await getNameAsync() }}
in the page, because Vue templates can't parse Promise
directly.
<template>
<div>
<p>Hi {{ name }}</p>
</div>
</template>
<script setup lang="ts">
const name = ref<string>('')
async function getNameAsync() {
return 'John'
}
onMounted(async() => {
name.value = await getNameAsync()
})
</script>
本文标签: asynchronousIs there a way to use async functions in a Vue expression templateStack Overflow
版权声明:本文标题:asynchronous - Is there a way to use async functions in a Vue expression template? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741297041a2370878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论