如果在进入详情页后,父级菜单高亮丢失,可能是由于路由配置不正确或者是el-menu组件的active-name属性没有设置正确导致的。

可以尝试以下方法解决:

  1. 确认路由配置是否正确,详情页的路由应该包含父级菜单所在的路由路径,例如:
{
  path: '/parent',
  component: ParentComponent,
  children: [
    {
      path: 'child',
      component: ChildComponent
    }
  ]
}
  1. 确认el-menu组件的active-name属性是否设置正确,应该设置为当前路由对应的菜单的name值,例如:
<el-menu :default-active="$route.name" class="el-menu-vertical-demo" :collapse="isCollapse">
  <el-menu-item index="parent">
      <i class="el-icon-location"></i>
      <span>父级菜单</span>
  </el-menu-item>
  <el-menu-item index="child">
      <i class="el-icon-menu"></i>
      <span>子级菜单</span>
  </el-menu-item>
</el-menu>
  1. 如果以上两个方法都无效,可以尝试手动设置父级菜单的高亮状态,在created钩子函数中监听路由变化,根据当前路由的路径设置父级菜单的active属性,例如:
created() {
  this.$router.afterEach((to, from) => {
    if (to.path.indexOf('/parent') === 0) {
      this.activeMenu = 'parent';
    } else {
      this.activeMenu = '';
    }
  });
}

其中activeMenu是data中的一个变量,用于控制el-menu组件的active属性。

vue el-menu 路由进入详情页父级高亮丢失

原文地址: https://www.cveoy.top/t/topic/bgjL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录