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

 <el-tabs v-model="activeIndex" type="border-card" @tab-click="tabClick" @tab-remove="tabRemove">
<el-tab-pane :closable="item.name == '首页'?false:true" :key="item.name" v-for="(item) in options" :label="item.name" :name="item.route">
</el-tab-pane>
</el-tabs>

methods:

  // tab 切换时, 动态的切换路由
tabClick(tab) {
this.$router.push({
path: this.activeIndex
}); // 路由跳转
}, tabRemove(targetName) {
this.$store.dispatch('menu/deleteTabs', targetName);
// 还同时需要处理一种情况当需要移除的页面为当前激活的页面时, 将上一个 tab 页作为激活 tab
if (this.activeIndex === targetName) {
// 设置当前激活的路由
if (this.options && this.options.length >= ) {
this.$store.dispatch('menu/setActiveIndex', this.options[this.options.length - ].route);
this.$router.push({
path: this.activeIndex
});
}else{
this.$router.push('/home')
}
}
}

数据是存放在vuex中的:

 computed: {
options: {
get () {return this.$store.state.menu.options},
set (val) {this.$store.dispatch('menu/addTabs', val)}
},
// 动态设置及获取当前激活的 tab 页
activeIndex: {
get() {
return this.$store.state.menu.activeIndex;
},
set(val) {
this.$store.dispatch('menu/setActiveIndex', val);
}
}
},

mounted:

 mounted() {
let options = JSON.parse(window.localStorage.getItem('menuOptions'))
this.activeIndex = localStorage.getItem('menuIndex')
if(!options) {
options = []
this.$router.push('/home')
this.$store.commit('menu/SET_ACTIVEI_NDEX', options.route)
// this.$store.dispatch('menu/setActiveIndex', options.route)
}else {
this.$store.commit('menu/SET_OPTIONS', options)
}
//this.$store.dispatch('menu/setActiveIndex', options.route)
//this.$store.commit('menu/SET_OPTIONS', options)
// 设置当前激活的路由
if (options && options.length >= ) {
for(var i = ; i < options.length; i++){
if(options[i].route == this.activeIndex){
this.$store.dispatch('menu/setActiveIndex', options[i].route) }
}
}else{
this.$router.push('/home')
}
},

store/menu.js

 const state = {
options: [{ route: '/home', name: '首页' }],
activeIndex: '/home'
}
const mutations = {
SET_OPTIONS: (state, data) => {
state.options = data
},
// 添加 tabs
ADD_TABS: (state, data) => {
//state.options.findIndex(m => m.name === data.name) < 0 && state.options.push(data)
state.options.push(data);
localStorage.setItem("menuOptions", JSON.stringify(state.options))
},
// 删除 tabs
DELETE_TABS: (state, route) => {
let index = ;
for (let option of state.options) {
if (option.route === route) {
break;
}
index++;
}
state.options.splice(index, );
localStorage.setItem("menuOptions", JSON.stringify(state.options))
},
// 设置当前激活的 tab,route
SET_ACTIVEI_NDEX: (state, index) => {
state.activeIndex = index;
localStorage.setItem("menuIndex", state.activeIndex)
},
}
const actions = {
addTabs({ commit }, info) {
commit('ADD_TABS', info)
},
deleteTabs({ commit }, info) {
commit('DELETE_TABS', info)
},
setActiveIndex({ commit }, info) {
commit('SET_ACTIVEI_NDEX', info)
},
} export default {
namespaced: true,
state,
mutations,
actions
}

over!

最新文章

  1. MySQL的Explain命令
  2. [转]oracle设计数据库应选择正确的数据类型
  3. session和cookie区别
  4. V-rep学习笔记:曲柄摇杆机构
  5. 转: https 单向双向认证说明_数字证书, 数字签名, SSL(TLS) , SASL
  6. Part 9 Sorting data in AngularJS
  7. install xdebug on fedora
  8. 快速学习使用 Windows Azure 上的 SharePoint Server 2013
  9. BootStrap 轮播 Carousel
  10. 解决tomcat部署项目中碰到的几个问题
  11. 『骑士精神 IDA*』
  12. java基础1之引用数据类型
  13. vuejs2+webpack2+vuxui2多页面架手脚,支持二级目录
  14. Shell-6--预定义变量
  15. Redhat6.5——解决yum功能不能正常使用
  16. [ERROR] InnoDB: Trying to access page number 7 in space 957, space name XXX which is outside the tablespace bounds
  17. vue学习七之Axios
  18. linux 101 hacks 6history 及相关关键字
  19. 如何解决谷歌Chrome浏览器空白页的问题
  20. 每日英语:No Consensus: China Debate on Women&#39;s Roles

热门文章

  1. try中定义的变量在finally中找不到
  2. 最简化的DirectX 11开发环境的配置 VS2010
  3. java的Junit单元测试
  4. fetch API 和 ajax
  5. python 堡垒机讲解及实例
  6. ehcache缓存框架之二级缓存
  7. Kafka常用命令及配置文件
  8. JAVA:初识Java &middot; Xer97
  9. lsync+rsync 实时同步(ubuntu16.04系统)
  10. 机器学习 &mdash;&mdash; 数据预处理