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 scoped>
.active{
background:#f1f4f5;
}
</style>