首页 技术 正文
技术 2022年11月17日
0 收藏 412 点赞 2,530 浏览 2579 个字

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!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894