博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
element UI中的tab切换栏
阅读量:4554 次
发布时间:2019-06-08

本文共 3744 字,大约阅读时间需要 12 分钟。

html代码:(用的是el-tab组件)

1 
2
3
4

methods:

1  // tab 切换时, 动态的切换路由 2       tabClick(tab) { 3         this.$router.push({ 4           path: this.activeIndex 5         }); // 路由跳转 6       }, 7  8  9 tabRemove(targetName) {10         this.$store.dispatch('menu/deleteTabs', targetName);11         // 还同时需要处理一种情况当需要移除的页面为当前激活的页面时, 将上一个 tab 页作为激活 tab12         if (this.activeIndex === targetName) {13           // 设置当前激活的路由14           if (this.options && this.options.length >= 1) {15             this.$store.dispatch('menu/setActiveIndex', this.options[this.options.length - 1].route);16             this.$router.push({17               path: this.activeIndex18             });19           }else{20             this.$router.push('/home')21           }22         }23       }

数据是存放在vuex中的:

1 computed: { 2       options: { 3         get () {
return this.$store.state.menu.options}, 4 set (val) {
this.$store.dispatch('menu/addTabs', val)} 5 }, 6 // 动态设置及获取当前激活的 tab 页 7 activeIndex: { 8 get() { 9 return this.$store.state.menu.activeIndex;10 },11 set(val) {12 this.$store.dispatch('menu/setActiveIndex', val);13 }14 }15 },

mounted: 

1 mounted() { 2         let options = JSON.parse(window.localStorage.getItem('menuOptions')) 3         this.activeIndex = localStorage.getItem('menuIndex') 4         if(!options) { 5           options = [] 6           this.$router.push('/home') 7           this.$store.commit('menu/SET_ACTIVEI_NDEX', options.route) 8           // this.$store.dispatch('menu/setActiveIndex', options.route) 9           }else {10             this.$store.commit('menu/SET_OPTIONS', options)11           }12           //this.$store.dispatch('menu/setActiveIndex', options.route)13           //this.$store.commit('menu/SET_OPTIONS', options)14         // 设置当前激活的路由15         if (options && options.length >= 1) {16           for(var i = 0; i < options.length; i++){17             if(options[i].route == this.activeIndex){18               this.$store.dispatch('menu/setActiveIndex', options[i].route)19               20             }21           }22         }else{23           this.$router.push('/home')24         }25        },

store/menu.js

1 const state = { 2     options: [{ route: '/home', name: '首页' }], 3     activeIndex: '/home' 4 } 5 const mutations = { 6     SET_OPTIONS: (state, data) => { 7         state.options = data 8     }, 9     // 添加 tabs10     ADD_TABS: (state, data) => {11         //state.options.findIndex(m => m.name === data.name) < 0 && state.options.push(data)12         state.options.push(data);13         localStorage.setItem("menuOptions", JSON.stringify(state.options))14     },15     // 删除 tabs16     DELETE_TABS: (state, route) => {17         let index = 0;18         for (let option of state.options) {19             if (option.route === route) {20                 break;21             }22             index++;23         }24         state.options.splice(index, 1);25         localStorage.setItem("menuOptions", JSON.stringify(state.options))26     },27     // 设置当前激活的 tab,route28     SET_ACTIVEI_NDEX: (state, index) => {29         state.activeIndex = index;30         localStorage.setItem("menuIndex", state.activeIndex)31     },32 }33 const actions = {34     addTabs({ commit }, info) {35         commit('ADD_TABS', info)36     },37     deleteTabs({ commit }, info) {38         commit('DELETE_TABS', info)39     },40     setActiveIndex({ commit }, info) {41         commit('SET_ACTIVEI_NDEX', info)42     },43 }44 45 46 export default {47     namespaced: true,48     state,49     mutations,50     actions51 }

over!

 

转载于:https://www.cnblogs.com/lesliejavascript/p/11460641.html

你可能感兴趣的文章
js按条件分类json数组,并合计同组数据(一维转换为二维)
查看>>
Exp6 信息搜集与漏洞扫描
查看>>
redis4安装
查看>>
使用命令wsimport构建WebService客户端[转]
查看>>
第八遍:链接详解
查看>>
Qt5.5 使用smtp发邮件的各种坑
查看>>
js奇葩错误 字符串传递问题
查看>>
人之初,性本恶
查看>>
springboot 端口号
查看>>
使用AChartEngine画动态曲线图
查看>>
安卓项目五子棋代码详解(四)
查看>>
urllib 学习一
查看>>
bzoj4196 [Noi2015]软件包管理器——树链剖分
查看>>
kafka源码阅读环境搭建
查看>>
UI设计
查看>>
androidtab
查看>>
Windows Phone 自定义弹出框和 Toast 通知
查看>>
如何生成静态页面的五种方案
查看>>
php 事件驱动 消息机制 共享内存
查看>>
剑指offer 二叉树的bfs
查看>>