关于VUE + Element 侧边栏页面刷新后不高亮显示的问题

24 篇文章 0 订阅
14 篇文章 1 订阅

写后台管理系统 我们会经常用到element,侧边栏导航的时候路由跳转没有问题 但之后页面刷新发现侧边栏的选中状态(高亮显示)没有了,针对这个问题特别提出以下见解:


直接贴代码:

<el-menu
      :default-active="$route.path"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      text-color="#333"
    >
      <el-menu-item  index="/situation" @click="go('/situation')"
        ><img src="../assets/img/qyts.png" alt="" /><span class="oneName"
          >全员态势</span
        ></el-menu-item
      >
      <el-submenu index="2" @click="go()">
        <template slot="title">
          <!-- <i class="el-icon-user"></i> -->
          <img src="../assets/img/jcsj.png" alt="" />
          <span class="oneName">基础数据</span>
        </template>
        <el-menu-item-group>
          <el-menu-item index="/healthTable" @click="go('/healthTable')" class="twoName"
            >心率监测</el-menu-item
          >
          <el-menu-item index="/healthsleep" class="twoName" @click="go('/healthsleep')">活动状态</el-menu-item>
          <el-menu-item index="/healthmovement" class="twoName" @click="go('/healthmovement')">运动监测</el-menu-item>
          <el-menu-item index="/healthoxygen" class="twoName" @click="go('/healthoxygen')">血氧监测</el-menu-item>
          <el-menu-item index="/temperature" class="twoName" @click="go('/temperature')">体温监测</el-menu-item>
          <el-menu-item index="/police" class="twoName" @click="go('/police')">电子围栏</el-menu-item>
          <el-menu-item index="/Staydetection" class="twoName"  @click="go('/Staydetection')">停留监测</el-menu-item>
        </el-menu-item-group>
      </el-submenu>
      <el-menu-item index="/person" @click="go('/person')">
        <img src="../assets/img/grxx.png" alt="" />
        <span class="oneName">个人信息</span>
      </el-menu-item>
</el-menu>

这里使用了:
router模式,启用该模式会在激活导航时以 index 作为 path 进行路由跳转,所以我将el-menu-item的index设置成了path路由
default-active则是当前激活菜单的index。

之后这里利用了以上两点,将default-active绑定为$route.path,例如当前页面是全员态势,那么就是/situation。这样即使刷新也不会失去高亮显示状态。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中使用Element UI实现侧边导航循环遍历icon的实现方法与上面所示的基本类似,只是需要使用Element UI提供的icon组件。以下是一个示例代码: ```html <template> <div> <el-menu :default-active="activeIndex" class="el-menu-vertical-demo" @select="handleSelect"> <el-menu-item v-for="(item, index) in navItems" :key="index" :index="index"> <i :class="item.icon"></i> <span slot="title">{{ item.title }}</span> </el-menu-item> </el-menu> </div> </template> <script> export default { data() { return { activeIndex: "0", // 默认选中项的索引 navItems: [ { title: "Home", link: "/", icon: "el-icon-s-home" // Element UI提供的icon类名 }, { title: "About", link: "/about", icon: "el-icon-info" }, { title: "Contact", link: "/contact", icon: "el-icon-message" }, // 循环遍历的数据可以自行添加或从后端API获取 ] }; }, methods: { handleSelect(index) { this.activeIndex = index; } } }; </script> ``` 在上面的代码中,`navItems`数组中存储了侧边导航的数据,包括每个导航项的标题、链接和图标类名。使用`v-for`指令在模板中循环遍历这些导航项,并将每个图标的类名绑定到`<i>`元素的`class`属性上,以显示相应的图标。同时,使用Element UI提供的`el-menu`和`el-menu-item`组件实现了导航菜单的基本结构,并使用`default-active`属性指定默认选中项的索引。在`handleSelect`方法中,我们可以监听导航菜单的`select`事件,更新选中项的索引并执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值