Vue3: 使用vue-router/auto-routes情况下为不存在的路由返回404页面

src/router/main.ts

参考示例,提前写好一个用于展示的页面放到src/pages/404.vue

import { createRouter, createWebHashHistory } from 'vue-router/auto'
import { setupLayouts } from 'virtual:generated-layouts'
import { routes } from 'vue-router/auto-routes'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    ...setupLayouts(routes),
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/pages/404.vue')
    }
  ],
})

router.isReady().then(() => {
  localStorage.removeItem('vuetify:dynamic-reload')
})

export default router
暂无评论

发送评论 编辑评论


				
上一篇
下一篇