当我使用 async setup ()
在 Vue 3 中,我的组件消失了。我使用了这里找到的解决方案:
why i got blank when use async setup() in Vue3 ...它有效,但是当我使用 router-view
时出现空白页.
<template>
<div v-if="error">{{error}}</div>
<Suspense>
<template #default>
<router-view></router-view>
</template>
<template #fallback>
<Loading />
</template>
</Suspense>
</template>
<script>
import Loading from "./components/Loading"
import { ref, onErrorCaptured } from "vue"
export default {
name: 'App',
components: { Loading },
setup() {
const error = ref(null)
onErrorCaptured(e => {
error.value = e
})
}
}
</script>
主.js:import { createApp } from 'vue'
import router from "./router"
import App from './App.vue'
createApp(App).use(router).mount('#app')
当我更换 router-view
使用我的一个组件,它出现了。路由器:
import { createWebHistory, createRouter } from "vue-router";
import Portfolio from "@/views/Portfolio.vue";
import Blog from "@/views/Blog/Blog.vue";
import Detail from "@/views/Blog/Detail.vue";
import NotFound from "@/views/NotFound.vue";
const routes = [
{
path: "/",
name: "Home",
component: Portfolio,
},
{
path: "/blog",
name: "blog",
component: Blog,
},
{
path: "/blog/:slug",
name: "detail",
component: Detail,
},
{
path: "/:catchAll(.*)",
component: NotFound,
},
];
const router = createRouter({
history: createWebHistory(),
routes,
});
export default router;
最佳答案
<Suspense>
的默认模板应该包含一个异步组件(即,一个带有 async setup()
的组件),但是您已经放置了 <router-view>
而是在那里。
您必须重构 <Suspense>
到包含异步组件的自己的包装器组件(例如 HomeView.vue
)中,留下 <router-view>
本身在 App
:
<!-- App.vue -->
<template>
<router-view />
</template>
<!-- HomeView.vue -->
<template>
<Suspense>
<template #default>
<MyAsyncComponent />
</template>
<template #fallback>
<Loading />
</template>
</Suspense>
</template>
然后更新您的路由器配置以使用该包装器:const routes = [
{
path: '/'
name: 'Home',
component: HomeView
},
//...
]
关于javascript - 在带有路由器 View 的 Vue3 中使用 async setup() 时出现空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64278009/