vue3中路由错误自动跳转404页面 路由表写法

发布时间 2023-04-06 16:11:04作者: 南城0
  1. 定义路由表
import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
	{
	 path: "/",
	 name: "home",
	 component: Home,
	},
 // ...
	{
	 path: "/404",
	 name: "404",
	 component: () =>
	  import(/* webpackChunkName: "404" */ "../views/404"),
	},
	{
	 // 此处vue3更新需要使用catchall方法匹配,不然会出现错误:
	 // Catch all routes ("*") must now be defined using a param with a custom regexp
	 path: "/:catchAll(.*)",
	 redirect: "/404",
	},
]