Element UI导航菜单刷新就复原问题解决方法~

1、首先要知道为什么一刷新就复原了,是因为default-active属性设置的是默认值,是一个死值,一旦刷新就会复原,造成高亮不能保持,那么怎么解决呢?

2、很简单,无需像一些博主一样绑定path。思路:既然一刷新就复原了,那么我们在切换的时候就将值存储在本地存储或vuex中呗。

方法一:本地存储

javascript 复制代码
<el-menu
  :default-active="activeIndex2" class="el-menu-demo" mode="horizontal"
  @select="handleSelect" background-color="#545c64" text-color="#fff"
  active-text-color="#ffd04b"
>
    <el-menu-item :index="String(index)" v-for="(item,index) in list" :key="item.id">{{ item.name }}</el-menu-item>
    <el-submenu index="4">
      <template slot="title">我的工作台</template>
      <el-menu-item index="4-1">设置</el-menu-item>
      <el-menu-item index="4-2" >退出</el-menu-item>
    </el-submenu>
</el-menu>

<script>
export default {
  name: "showTop",
  data() {
    return {
      list: [{name:"首页",id:0},{name:"商品",id:1},{name:"订单",id:2},{name:"会员",id:3},{name:"设置",id:4},{name:"管理员",id:5}],
      activeIndex2: null
    };
  },
  mounted(){
    if(localStorage.getItem('activeIndex2')){
      this.activeIndex2 =localStorage.getItem('activeIndex2')
    }else{
      this.activeIndex2 =String(this.list[0].id)
    }
  },
  methods: {
    // 顶部导航栏点击事件
    handleSelect(key, keyPath) {
        console.log(key,keyPath);
        // 解决element ui组件库中顶部导航栏刷新复原问题,也可存储在vuex中
        localStorage.setItem('activeIndex2',String(this.list[key].id))
        // 判断是否是退出按钮
        if(key=='4-2'){
          // 清空本地缓存的token
          localStorage.removeItem('token')
          // 跳转至登录页
            this.$router.push({name:'login'})
        }
      }
  },
};
</script>

方法二:vuex+本地存储

javascript 复制代码
<el-menu
  :default-active="activeIndex2" class="el-menu-demo" mode="horizontal"
  @select="handleSelect" background-color="#545c64" text-color="#fff"
  active-text-color="#ffd04b"
>
    <el-menu-item :index="String(index)" v-for="(item,index) in list" :key="item.id">{{ item.name }}</el-menu-item>
    <el-submenu index="4">
      <template slot="title">我的工作台</template>
      <el-menu-item index="4-1">设置</el-menu-item>
      <el-menu-item index="4-2" >退出</el-menu-item>
    </el-submenu>
</el-menu>

<script>
import { mapMutations} from 'vuex';
export default {
  name: "showTop",
  data() {
    return {
      list: [{name:"首页",id:0},{name:"商品",id:1},{name:"订单",id:2},{name:"会员",id:3},{name:"设置",id:4},{name:"管理员",id:5}],
      activeIndex2: null
    };
  },
  mounted(){
    if(localStorage.getItem('activeIndex2')){
      this.activeIndex2 =localStorage.getItem('activeIndex2')
    }else{
      this.activeIndex2 =String(this.list[0].id)
    }
  },
  methods: {
    ...mapMutations(['setActiveIndex2']),
    // 顶部导航栏点击事件
    handleSelect(key, keyPath) {
        console.log(key,keyPath);
        // 存储到vuex中,但是要记住还需要在vuex中存储到本地存储中
        this.setActiveIndex2(String(this.list[key].id))
        // 判断是否是退出按钮
        if(key=='4-2'){
          // 清空本地缓存的token
          localStorage.removeItem('token')
          // 跳转至登录页
            this.$router.push({name:'login'})
        }
      }
  },
};
</script>
javascript 复制代码
//store中的index.js
import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)

export default new Vuex.Store({
    state:{
        activeIndex:'',
    },
    mutations:{
        setActiveIndex2(state,e){
            state.activeIndex = e
            window.localStorage.setItem('activeIndex2',e)
        }
    },
})
javascript 复制代码
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from '../router';
import store from '../store';
Vue.use(ElementUI);

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')

效果图:

相关推荐
To_OC2 小时前
LC 1 两数之和:面试第一道必考题,暴力解法直接被面试官 pass
javascript·算法·leetcode
GuWenyue3 小时前
排序效率低?5分钟吃透快速排序,性能飙升至O(nlogn)
前端·javascript·面试
OpenTiny社区3 小时前
🎨 看完 GenUI SDK 源码我悟了!
前端·vue.js·github
何时梦醒3 小时前
深入理解递归与快速排序 —— 从基础入门到手写实现
前端·javascript
bonechips4 小时前
LLM 的无状态:从 HTTP 协议到对话上下文工程
前端·javascript
胡志辉4 小时前
从 prototype 到 V8,看懂 JavaScript 原型链
前端·javascript
mqcode5 小时前
你项目里的 axios,封对了吗?从裸用到生产级的四步进化
vue.js·axios
ping某5 小时前
专栏-null 和 undefined 到底是什么?
前端·javascript·后端
Linsk6 小时前
组件 = 模板 + 业务逻辑
java·前端·vue.js
swipe8 小时前
从 0 到 1 理解 React 虚拟列表:定高、不定高与 Canvas 版本完整拆解
前端·javascript·面试