现在有一个需求,就是页面刷新的时候,我需要判断当前url是否在路由规则(集合)里面?如果存在,就定位到当前路由,否则跳转到首页。
假设当前路由为:/xxx/yyy/100
定义的路由里面有:/xxx/yyy/:id
刷新页面时,我需要使用/xxx/yyy/100
匹配到/xxx/yyy/:id
,这个时候我如何能够方便的忽略路由参数来比较呢?
自己方法如下:
let currentRoute = this.$route
let currentRoutePath = currentRoute.path.toLowerCase()
if(currentRoute.params) {
for (const key in currentRoute.params) {
if (currentRoute.params.hasOwnProperty(key)) {
const element = currentRoute.params[key];
currentRoutePath = currentRoutePath.replace(element, `:${key}`)
}
}
}
也就是把现有路由(赋值后)转成路由规则(赋值前),如:
当前路由
/xxx/yyy/ccc8a6d0-3e86-465e-a255-4bf4e3735b79
转成成
/xxx/yyy/:id
然后进行匹配,不知道时候有问题(还未经过长时间验证)。
有更好的方法吗?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…