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


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

實現效果:

實現代碼:在vuex里面定義tags存放所有打開的菜單,和當前打開的索引curtagsIndex:,通過遍歷tags,看是否等於當前索引

1.在store的index.js文件里面定義tags和curtagsIndex

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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM