使用/:pathMatch(.*)或者/:catchAll(.*)
Catch all routes (“*”) must now be defined using a param with a custom regexp.
当访问url时,访问没有配置的路由时,默认显示404页面,浏览器报错
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'home',
component: () => import('../layout/layout.vue'),
children: [
{
path: 'about', // 这里不要加上/about,使用就找不到页面
name: 'about',
component: () => import('../views/AboutView.vue'),
},
],
},
{
path: '*',
redirect: '/404',
children: [
{
path: '/404', // 这里要加上/404,和上面的redirect对应,必须加上/
name: '404',
component: () => import('../views/404/404.vue'),
},
],
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
export default router;
使用/:pathMatch(.*)或者/:catchAll(.*)
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'home',
component: () => import('../layout/layout.vue'),
children: [
{
path: 'about', // 这里不要加上/about,使用就找不到页面
name: 'about',
component: () => import('../views/AboutView.vue'),
},
],
},
{
path: '/:pathMatch(.*)',
redirect: '/404',
children: [
{
path: '/404', // 这里要加上/404,和上面的redirect对应,必须加上/
name: '404',
component: () => import('../views/404/404.vue'),
},
],
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
export default router;
之前的children里面需要加上’/’的问题解决,把children里面提出来
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'home',
component: () => import('../layout/layout.vue'),
children: [
{
path: 'about', // 这里不要加上/about,使用就找不到页面
name: 'about',
component: () => import('../views/AboutView.vue'),
},
],
},
{
path: '/404',
name: '404',
component: () => import('../views/404/404.vue'),
},
{
path: '/:pathMatch(.*)',
redirect: '/404',
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
export default router;
原文地址:https://blog.csdn.net/qq_54455551/article/details/129236827
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_15531.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!