avatar

麦兜的小站

MDO.INK

  • 首页
  • 随笔
  • 知识库
  • 归档
  • 动态
  • 标签
  • 关于
Home 适用于vue3的vue-router的404配置
文章

适用于vue3的vue-router的404配置

Posted 2025-02-3 Updated 2025-02- 3
By power 已删除用户
4~6 min read

老版本(适用于vue2的vue-router)

在所有路由的最后配置

{
    path: '*',
    name: '404',
    component: () => import('@/pages/404')
}

新版本(适用于vue3的vue-router)

{
    path: '/404',
    name: '404',
    component: () => import('@/pages/404')
},
{
    path: '/:pathMatch(.*)',
    redirect: '/404'
}

相对完整的示例

import { RouteRecordRaw } from 'vue-router'
import LoginPage from 'pages/LoginPage.vue'
import BasicLayout from 'layouts/BasicLayout.vue'
import { LOGIN_ROUTE_NAME } from 'utils/SystemConstants'


const component404 = import('pages/Page404.vue')

const rootRoutePage404: RouteRecordRaw[] = [
  {
    path: '/404',
    component: () => component404,
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404',
  },
]

const indexSubRoute404: RouteRecordRaw[] = [
  {
    path: '404',
    component: () => component404,
  },
  {
    path: '/:pathMatch(index/.*)',
    redirect: '/index/404',
  },
]
const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: LOGIN_ROUTE_NAME,
    component: LoginPage,
  },
  {
    path: '/index',
    component: BasicLayout,
    redirect: '/index/home',
    children: [
      {
        path: 'home',
        name: 'HomePage',
        component: () => import('pages/HomePage.vue'),
      },
      {
        path: 'form/json-form',
        name: 'JsonFormPage',
        component: () => import('pages/all-demo/form-demo/JsonFormPage.vue'),
      },
      {
        path: 'form/el-form',
        name: 'ElFormPage',
        component: () => import('pages/all-demo/form-demo/ElFormPage.vue'),
      },
      {
        path: 'form/requirement-el-form',
        name: 'RequirementSimpleDynForm',
        component: () =>
          import('pages/all-demo/form-demo/RequirementSimpleDynFormPage.vue'),
      },
      ...indexSubRoute404,
    ],
  },
  ...rootRoutePage404,
]

export default routes
前端
License:  CC BY 4.0
Share

Further Reading

Jun 25, 2025

Vue实现顶部导航跟随页面联动效果

需求背景 最近也是应我一位同学的要求,给他的公司制作一个官网,那也就是拾起了一些基础的知识,搜索了一些其它的文章有实现这种效果的,但大多导航都是在侧边,然后点击导航对应的部分内容滑到最上面,跟我这次设...

Jun 20, 2025

Share.js - 一键分享到微博、QQ空间、QQ好友、微信、腾讯微博、豆瓣、Facebook、Twitter、Linkedin、Google+、点点

一键分享到微博、QQ空间、QQ好友、微信、腾讯微博、豆瓣、Facebook、Twitter、Linkedin、Google+、点点等社交网站,使用字体图标。 有两种安装方式: 使用npm npmi...

Jun 19, 2025

node express实现热更新

在使用node进行开发的时候,每次修改文件,都需要重启express服务很麻烦 我们可以使用nodemon这个库,修改文件后可以自动重启express服务。 安装: npminstall--save-...

OLDER

vue-hisory项目在nginx下的伪静态配置

NEWER

AXIOS构建请求处理全局loading状态

Recently Updated

  • 如何实现接口幂等性
  • 10个npm工具包
  • How to set up PHP7.4 on MacOS.
  • Automa:一键自动化,网页数据采集与工作流程优化专家Automa:解锁自动化
  • Mac 下用 brew 搭建 LNMP

Trending Tags

thinkphp clippings

Contents

©2025 麦兜的小站. Some rights reserved.

Using the Halo theme Chirpy