当我在Vue 3中使用async setup ()
时,我的组件消失了。为什么我在Vue3中使用async setup()时会出现空白...它工作正常,但当我使用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;
2条答案
按热度按时间ars1skjm1#
<Suspense>
的默认模板应该包含一个 *async组件 *(即一个带有async setup()
的组件),但您却将<router-view>
放在了那里。您必须将
<Suspense>
重构到它自己的包含异步组件的 Package 器组件(例如HomeView.vue
)中,将<router-view>
本身保留在App
中:然后更新路由器配置以使用该 Package 器:
nhaq1z212#
Vue文档中有一个示例,说明如何将
<Suspense>
与其他组件一起使用参考:https://vuejs.org/guide/built-ins/suspense.html#combining-with-other-components