element el-menu选中后刷新页面 保持当前页选中状态且选中高亮

 一定要加上router属性,启用该模式会在激活导航时以 index 作为 path 进行路由跳转,

default-active属性代表当前激活菜单的 index,所以想让哪个页面作为项目一进来的首页,就写哪个页面的路径

<el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect" router
      background-color="#666666" text-color="#fff" active-text-color="#fff">
      <el-menu-item index="/user/userList">运营中心</el-menu-item>
      <el-menu-item index="/user/userDetails">行业宝</el-menu-item>
      <el-menu-item index="/user/applicationList">企业宝</el-menu-item>
    </el-menu>

 

data() {
    return {
      activeIndex: '/user/userList',
    };
  },
  mounted() {
    this.activeIndex = this.$route.path
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    }
  },

项目的需求是要有选中的背景色且去掉下边框

.el-menu-item.is-active {
  background-color: #333333 !important;
  border-bottom: 0;
}

.el-menu-item {
  border-bottom: 0;
}

这是我的项目图

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
如果您使用的是 Element UI 的 el-menu 组件,在页面刷新高亮状态不会被保留。这是因为页面刷新后,Vue 组件会被重新渲染,组件的状态会被重置。 为了解决这个问题,您可以在组件的 mounted 钩子函数中获取当前页面的路由信息,然后设置 el-menu 的当前选中项。 具体操作步骤如下: 1. 导入 Vue Router 和 Element UI 组件。 ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import { Menu, Submenu, MenuItem } from 'element-ui' ``` 2. 注册 Vue Router 和 Element UI 组件。 ```javascript Vue.use(VueRouter) Vue.use(Menu) Vue.use(Submenu) Vue.use(MenuItem) ``` 3. 在组件的 mounted 钩子函数中获取当前页面的路由信息,并设置 el-menu 的当前选中项。 ```javascript mounted() { // 获取当前路由信息 const currentPath = this.$route.path // 设置 el-menu 的当前选中项 const menu = this.$refs.menu const activeItem = menu.$el.querySelector(`.el-menu-item[data-path="${currentPath}"]`) if (activeItem) { menu.setCurrentIndex(activeItem.getAttribute('data-index')) } } ``` 4. 在 el-menu 中设置 data-path 属性,用于在 mounted 钩子函数中获取当前页面的路由信息。 ```html <el-menu ref="menu"> <el-menu-item index="1" data-path="/home">首页</el-menu-item> <el-menu-item index="2" data-path="/about">关于我们</el-menu-item> <el-menu-item index="3" data-path="/contact">联系我们</el-menu-item> </el-menu> ``` 这样,当页面刷新时,el-menu 组件会自动设置当前选中项,保持高亮状态不变。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值