Vue-router-404页面

常规参数只匹配url片段时间的字符,用/分割。如果我们想匹配任意路径,我们可以使用自定义的路径参数正则表达式,在路径参数后面的括号中加入正则表达式。

如果输入的路径错误则跳转到404页面

首先,在src文件夹下的components文件夹中创建NotFound.vue文件,如图是目录列表:

 在NotFound.vue文件中代码如下:

<template>
 <h2>
     找不到页面
 </h2>
</template>

在router文件下的index.js文件中代码如下:

import { createRouter,createWebHashHistory } from "vue-router";

const routes=[{
    path:"/home",component:()=>import("../components/Home.vue")
},{
    path:"/about",component:()=>import("../components/About.vue")
},{
    path:"/user/:id",component:()=>import("../components/User.vue")
},{
    //使用正则的方式,匹配任意的
    path:"/:path(.*)",component:()=>import("../components/NotFound.vue")
}
]
const router =createRouter({
    history:createWebHashHistory(),
    routes,
})
export default router

结果如下:

 解决了如果因路径输入错误,从而都跳转到404页面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值