element-ui中點擊菜單,改變當前菜單背景顏色

需求: vue項目中,點擊左側菜單,tags頁顯示當前打開的菜單,而且高亮顯示當前菜單vue

實現效果:vuex

實現代碼:在vuex裏面定義tags存放全部打開的菜單,和當前打開的索引curtagsIndex:,經過遍歷tags,看是否等於當前索引less

1.在store的index.js文件裏面定義tags和curtagsIndexthis

const store = new Vuex.Store({ modules: { tags:[], curTagsIndex:"-1", }, mutations: { SET_CURTAGS: (state, index) => { state.curTagsIndex = index }, } })  

 

<div class="tag_list" v-for="(tag,index) in tags" @click="jump(tag)" :class="{ active:curTagsIndex == index}">
        <span>{{tag.name}}</span>
        <i class="el-icon-close" @click="close(tag)"></i>
      </div>



<script> import { mapState, mapGetters,mapMutations } from "vuex"; export default{ computed: { ...mapGetters(["tags","curTagsIndex"]), }, methods:{ ...mapMutations(["SET_CURTAGS"]), jump(val){ this.$router.push(val.path); this.tags.forEach((item,index)=>{ if(item.name==val.name){ this.$store.commit("SET_CURTAGS",index); } }) }, }, } </script>
<style lang="less" scoped> .active{ background:#f1f4f5;

  }

</style>
相關文章
相關標籤/搜索