【Ruoyi】若依默认打开第一个子菜单,三级菜单时默认跳转到第一个子菜单
官方给出的答案是处理2级菜单的,当菜单为三级时就会出现问题,打开404。在ruoyi-vue中,默认打开第一个子菜单问题处理。
·
情景说明:
在ruoyi-vue中,默认打开第一个子菜单问题处理,官方文档给出的答案是处理2级菜单的,当菜单为三级时就会出现问题,打开404。
官方代码:(重点关注代码位置和补充的内容)
只处理2级菜单的情况
这是官方处理加入的代码:
let myRoutes = [];
if (this.childrenMenus && this.childrenMenus.length > 0) {
this.childrenMenus.map((item) => {
if (key == item.parentPath || (key == "index" && "" == item.path)) {
myRoutes.push(item);
}
});
}
setTimeout(() => {
if(myRoutes[0].path != this.$route.path) {
this.$router.replace({
path: myRoutes[0].path
})
} else {
this.$router.replace({
path: '/index'
})
}
}, 100)
三级菜单处理方式:(把官方写的setTimeout换成我这个)
具体代码位置(看红框和箭头指向):
逻辑:当有三级就拼接一级,没有就继续二级
对应上图位置加入的代码:
setTimeout(() => {
let ppth = "";
if (myRoutes[0].children) {
ppth = myRoutes[0].path + "/" + myRoutes[0].children[0].path;
} else {
ppth = myRoutes[0].path;
}
if (ppth != this.$route.path) {
this.$router.replace({
path: ppth,
});
} else {
this.$router.replace({
path: "/index",
});
}
}, 100);
不要漏了这部分:
let myRoutes = [];
if (this.childrenMenus && this.childrenMenus.length > 0) {
this.childrenMenus.map((item) => {
if (key == item.parentPath || (key == "index" && "" == item.path)) {
myRoutes.push(item);
}
});
}
关键点就是找到末级路由,替换掉当前路由。。。
更多推荐
所有评论(0)