经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue实现el-menu和el-tab联动的示例代码
来源:jb51  时间:2023/4/14 9:17:43  对本文有异议

vue通过el-menus和el-tabs联动,实现点击侧边栏,页面内显示一行tab以及点击tab切换路由

实现效果如下

实现思路 左侧边栏添加点击事件/设置el-menu的路由模式,然后监听路由的变化,拿到的路由去改变el-tabs绑定的属性,然后改变el-tab-pane循环的数组,然后设置el-tabs的点击/删除事件,最终实现联动 首先使用vuex定义公共状态openTab以及activeIndexTab 也就是循环的数组和当前高亮

  1. import Vue from "vue"
  2. import Vuex from "vuex"
  3. Vue.use(Vuex)
  4.  
  5. export default new Vuex.Store({
  6. state: {
  7. openTab: [],
  8. activeIndexTab: ''
  9. },
  10. mutations: {
  11. //添加tab事件
  12. add_tabs (state, data) {
  13. state.openTab.push(data)
  14. },
  15. //删除
  16. delete_tabs (state, name) {
  17. let index = 0
  18. for (let item of state.openTab) {
  19. if (item.name === name) {
  20. break
  21. }
  22. index++
  23. }
  24. state.openTab.splice(index, 1)
  25. },
  26. //设置高亮tab
  27. set_active_index (state, index) {
  28. state.activeIndexTab = index
  29. },
  30. },
  31. })

html模板

  1. <el-menu>
  2. <div v-for="(item, index) in menuList" :key="index">
  3. <el-menu-item :index="item.index" :class="{'isActive':activeIndex == item.index}" @click="routeTo(item)">
  4. <i :class="['icon', item.name]"></i>
  5. <span slot="title">{{ item.title }}</span>
  6. </el-menu-item>
  7. </div>
  8. </el-menu>
  9. <el-tabs v-model="activeIndexTab" type="card" @tab-click="clickTab" @tab-remove="removeTab" closable>
  10. <el-tab-pane v-for="item of openTab" v-if="openTab.length" :key="item.title" :label="item.title" :name="item.name">
  11. </el-tab-pane>
  12. </el-tabs>

定义data函数中要用到的属性

  1. data() {
  2. return {
  3. activeIndex: "",
  4. menuList:[
  5. {"index":"1","title":"商户资料管理","name":"meansManage"},
  6. {"index":"2","title":"商户订单管理","name":"payOrderManage"},
  7. {"index":"3","title":"商户报表管理","name":"reportManage"},
  8. ]
  9. }
  10. },

在vuex中取到el-tabs用到的属性

  1. computed: {
  2. openTab () {
  3. return this.$store.state.openTab
  4. },
  5. activeIndexTab: {
  6. get () {
  7. return this.$store.state.activeIndexTab
  8. },
  9. set (val) {
  10. this.$store.commit('set_active_index', val)
  11. }
  12. },
  13. },

路由配置信息如下

  1. {
  2. path: "/",
  3. component: frame,
  4. redirect: "/meansManage",
  5. children: [
  6. {
  7. path: "/meansManage",
  8. name: "meansManage",
  9. meta:{title:'商户资料管理'},
  10. component: () => import("../components/merchantManage/meansManage/index.vue")
  11. },
  12. {
  13. path: "/payOrderManage",
  14. name: "payOrderManage",
  15. meta:{title:'商户订单管理'},
  16. component: () => import("../components/merchantManage/payOrderManage/orderIndex.vue")
  17. },
  18. {
  19. path:'/reportManage',
  20. name:'reportManage',
  21. meta:{title:'商户报表管理'},
  22. component: () => import('../components/merchantManage/reportManage/index.vue')
  23. }
  24. ]
  25. },

随后监听路由变化在watch中

  1. watch:{
  2. '$route'(val){
  3. let flag = false
  4. this.openTab.forEach(tab => {
  5. if (val.path == tab.name) {
  6. this.$store.commit('set_active_index',val.path)
  7. flag = true
  8. return
  9. }
  10. })
  11. if (!flag) {
  12. this.$store.commit('add_tabs', {name: val.path , title: val.meta.title})
  13. this.$store.commit('set_active_index', val.path)
  14. }
  15. }
  16. },

上面的代码大概意思就是,如果openTab中已经存在这个路由,则直接设置高亮tab,如果不存在,则先添加路由信息到openTab中,然后再设置高亮

7. 当前页面刷新,需要保留一个tab也就是当前页的

  1. mounted(){
  2. this.$store.commit('add_tabs', {name: this.$route.path , title: this.$route.meta.title})
  3. this.$store.commit('set_active_index', this.$route.path)
  4. }

设置tab的点击事件

  1. clickTab (tab) {
  2. this.$router.push({path: this.activeIndexTab})
  3. },
  4. removeTab (target) { //target当前被点击的name属性
  5. if (this.openTab.length == 1) {
  6. return
  7. }
  8. this.$store.commit('delete_tabs', target)
  9. if (this.activeIndexTab === target) {
  10. // 设置当前激活的路由
  11. if (this.openTab && this.openTab.length >= 1) {
  12. this.$store.commit('set_active_index', this.openTab[this.openTab.length - 1].name)
  13. this.$router.push({path: this.activeIndexTab})
  14. }
  15. }
  16. },

最终完美实现想要的效果。

到此这篇关于vue实现el-menu和el-tab联动的示例代码的文章就介绍到这了,更多相关el-menu和el-tab联动内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号