報錯詳情:
setup function returned a promise, but no boundary was found in the parent component tree. A component with async setup() must be nested in a in order to be rendered. 翻譯: setup函數(shù)返回了一個promise,但沒有邊界在父組件樹中找到。 具有異步setup()的組件必須嵌套在中才能呈現(xiàn)。
報錯原因:
如果使用<script setup>,可以在頂部直接使用await,結(jié)果代碼會被編譯成 async setup()。
例如子組件A:
<script setup>
import {getAllFrd} from '@/api/addfriends'
let text = await getAllFrd()
</script >
這樣使用了async setup()的子組件,父組件需要和Suspense配合使用,否則子組件渲染不出來,報錯warn以上內(nèi)容。文章來源:http://www.zghlxwxcb.cn/news/detail-618496.html
解決:
父組件處理:文章來源地址http://www.zghlxwxcb.cn/news/detail-618496.html
<script setup>
import { defineAsyncComponent } from 'vue'
const A = defineAsyncComponent(() =>
import('@/components/A.vue')
)
// 注意:同一個組件例如A組件,只能用一種引入方式,要么 defineAsyncComponent,要么import,
//不能不同父組件一個import一個defineAsyncComponent,否則打包時粘包
</script >
<template>
<div class="index">
<Suspense>
<template #default>
<A />
</template>
</Suspense>
</div>
</template>
到了這里,關(guān)于問題記錄:A component with async setup()must be nested in a in order to be rendered.的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!