在vue3中使用router.addroute动态添加路由时,由于路由添加是异步的,第一次不会匹配到未添加的路由导致警告,先把404写在固定路由里面,用来捕获第一次未匹配到的路由,就不会有警告了,但是会出现一刷新就跳转404
{
path: '/404',
name: '404',
component: () => import('@/views/error/404.vue'),
hidden: true,
meta: { title: "找不到此页面" }
},
{
path: '/:pathmatch(.*)',
name: 'notfound',
hidden: true,
redirect: '/404',
},所以在router.beforeeach里面需要判断一下,userpermissionsstore是使用了pinia存routes
try {
const permissionsstate = userpermissionsstore()
// 判断是否添加成功
// permissionsstate.routes的数据在permissionsstate.getroutes()中设置
if (!permissionsstate.routes.length) {
// 获取要加载的路由
const accessroutes = await permissionsstate.getroutes()
accessroutes.foreach((item) => router.addroute(item))
// 这里是核心代码,因为第一次匹配不到会跳转404
// 所以跳转到404的时候再做一个操作
if (to.path == '/404' && to.redirectedfrom) {
next({ path: to.redirectedfrom.fullpath, replace: true })
} else {
next({ ...to, replace: true }) // 这里相当于push到一个页面 不在进入路由拦截
}
} else {
next() // 如果不传参数就会重新执行路由拦截,重新进到这里
}
} catch (err) {
next(`/login?redirect=${to.path}`)
}总结
到此这篇关于vue3.0动态路由no match found for location with path警告解决的文章就介绍到这了,更多相关vue3.0 no match found for location with path警告内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论