vue+elementUI点击菜单添加tab

目录


效果如下

在这里插入图片描述
这个功能主要是靠elementUI的tab+Vuex来实现的,下面就来介绍如何实现的

思路:首先我们正常点击菜单跳转页面是靠路由实现的,然后我们需要点击菜单生成tab页和设置显示的tab页,所以我们需要用Vuex定义一个数组options,用来存放添加进去的tab页,还有一个设置显示哪个tab页的属性activeIndex,所以在点击时我们把这个页面的信息存放到options里面,点击删除就把这个tab的信息从options里面删除,然后当我们每次点击时就把点击的那个tab页的设置为激活

思路导图
在这里插入图片描述

这是我的项目结构目录

在这里插入图片描述
Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块

首先在state.js定义属性值,role是用来判断是否调用方法的一个参数,其他的属性是我这个项目其他功能所需

export default {
	resturantName: '飞哥餐馆',
	jwt:null,
	options: [],
    activeIndex: '',
	showName:'show',
	role:""
}

getters.js设置获取值的方法

export default {
	getResturantName: (state) => {
		return state.resturantName;
	},
	getJwt: (state) => {
		return state.jwt;
	},
	getShowName:(state) => {
		return state.showName;
	},
	getOptions:(state) => {
		return state.options;
	},
	getRole:(state) =>{
		return state.role;
	}
}

mumations.js,这里删除数组里面的元素用的是splice这个函数

export default {
	setResturantName: (state, payload) => {
		state.resturantName = payload.resturantName;
	},
	setJwt: (state, payload) => {
		state.jwt= payload.jwt;
	},
	 // 添加tabs
        add_tabs(state, data) {
            this.state.options.push(data);
        },
        // 删除tabs
        delete_tabs(state, route) {
            let index = 0;
            for (let option of state.options) {
                if (option.route === route) {
                    break;
                }
                index++;
            }
            this.state.options.splice(index, 1);//删除options里面下标为Index的一个数
        },
        // 设置当前激活的tab
        set_active_index(state, index) {
            this.state.activeIndex = index;
        },
		//设置tab页显示标题
		set_showName(state,name){
			this.state.showName=name;
		},
		set_role(state,role){
			this.state.role=role;
		}
}

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import getters from './getters'
import actions from './actions'
import mutations from './mutations'

//   ./就是代表同层级

Vue.use(Vuex)

const store = new Vuex.Store({
 	state,
 	getters,
 	actions,
 	mutations
 })

 export default store

LeftNav.vue,左侧菜单

<template>	
	<el-menu router :default-active="$route.path" default-active="2" class="el-menu-vertical-demo" background-color="#334157" text-color="#fff"
	 active-text-color="#ffd04b" :collapse="collapsed">
		<!-- <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b"> -->
		<div class="logobox">
			<img class="logoimg" src="../assets/img/logo.png" alt="">
		</div>
		<!-- 注意:index是必填的属性  1、index可以看成ID,也就是说它是唯一的
					2、它代表路由的跳转路径-->
		<el-submenu :index="'id_'+m.treeNodeId" v-for="m in menus">
			<template slot="title">
				<i :class="m.icon"></i>
				<span>{{m.treeNodeName}}</span>
			</template>
			<!-- :index="m2.url" -->
			<el-menu-item :key="'id_'+m2.treeNodeId" :index="m2.url" @click="showName(m2.treeNodeName)" v-for="m2 in m.children">
				<template slot="title">
					<i :class="m2.icon"></i>
					<span>{{m2.treeNodeName}}</span>
				</template>
			</el-menu-item>
		</el-submenu>
	</el-menu>
	
</template>

<script>
	export default {
		data() {
			return {
				collapsed: false,
				menus: [],
				editableTabsValue: '2',
        tabIndex: 2
      }

		},
		created() {
			// 监听事件
			this.$root.Bus.$on("collapsed-side", (v) => {
				this.collapsed = v;
			});
			let url = this.axios.urls.SYSTEM_MENU_TREE;
			this.axios.post(url, {}).then((response) => {
				// console.log(response)
				this.menus = response.data.result
			}).catch((response) => {
				console.log(response)
			});
		},
		methods:{
			showName(name){
				// 把菜单名称放进去,当成tab页的名称
				this.$store.commit('set_showName', name)
				this.$store.commit('set_role', "pass");
			}
		}
	}
</script>
<style>
	.el-menu-vertical-demo:not(.el-menu--collapse) {
		width: 240px;
		min-height: 400px;
	}

	.el-menu-vertical-demo:not(.el-menu--collapse) {
		border: none;
		text-align: left;
	}

	.el-menu-item-group__title {
		padding: 0px;
	}

	.el-menu-bg {
		background-color: #1f2d3d !important;
	}

	.el-menu {
		border: none;
	}

	.logobox {
		height: 40px;
		line-height: 40px;
		color: #9d9d9d;
		font-size: 20px;
		text-align: center;
		padding: 20px 0px;
	}

	.logoimg {
		height: 40px;
	}
</style>

主页面,也将是tab显示页,AppMain.vue,功能主要都在这个页面

<template>
	<el-container class="main-container">
		<el-aside v-bind:class="asideClass">
			<LeftNav></LeftNav>
		</el-aside>
		<el-container>
			<el-header class="main-header">
				<TopNav></TopNav>
			</el-header>
			<div class="template-tabs">
				<el-tabs v-model="activeIndex" type="border-card" closable @tab-click="tabClick" @tab-remove="tabRemove">
                    <el-tab-pane :key="item.name" v-for="(item, index) in options" :label="item.name" :name="item.route">
                    </el-tab-pane>
                </el-tabs>
			</div>
			<el-main class="main-center">
				
				<router-view></router-view>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
	// 导入组件
	import TopNav from '@/components/TopNav.vue'
	import LeftNav from '@/components/LeftNav.vue'

	// 导出模块
	export default {
		data() {
			return {
				asideClass: 'main-aside',
				editableTabsValue: '',
				editableTabs: []
			}
		},
		components: {
			TopNav,LeftNav
		},
		created() {
			this.$root.Bus.$on("collapsed-side", (v) => {
				this.asideClass = v ? 'main-aside-collapsed' : 'main-aside';
			})
		},
		methods: {
            // tab切换时,动态的切换路由
            tabClick(tab) {
                let path = this.activeIndex;
                this.$router.push({ path: path });
								this.$store.commit('set_role',"nopass");
            },
            tabRemove(targetName) {
							this.$store.commit('set_role',"nopass");
							  // let tabs = this.editableTabs;
                this.$store.commit('delete_tabs', targetName);
								// 如果激活的关闭的tab就是激活的tab
                if (this.activeIndex === targetName) {
					// 设置当前激活的路由
                    if (this.options && this.options.length >= 1) {
                        this.$store.commit('set_active_index', this.options[this.options.length - 1].route);
                        this.$router.push({ path: this.activeIndex });
                    } 
					   else {
                        this.$router.push({ path: '/AppMain' });
                    }
                }
            }
        },
		 watch: {
			'$route'(to) {
				let role=this.$store.state.role;
					let showName=this.$store.getters.getShowName
						let flag = false;//判断是否页面中是否已经存在该路由下的tab页
						//options记录当前页面中已存在的tab页
						for (let option of this.options) {
						//用名称匹配,如果存在即将对应的tab页设置为active显示桌面前端
							if (option.name === showName) {
								flag = true;
								this.$store.commit('set_active_index',  to.path);
								break;
							}
						}
						//如果不存在,则新增tab页,再将新增的tab页设置为active显示在桌面前端
						// if(role!='nopass'){}
						if(role=='pass'){
							if (!flag) {
								this.$store.commit('add_tabs', { route: to.path, name: showName});
								this.$store.commit('set_active_index',  to.path);
							}
						}
			}
		},
		computed: {
			options() {
				return this.$store.state.options;
			},
			//动态设置及获取当前激活的tab页
			activeIndex: {
				get() {
					return this.$store.state.activeIndex;
				},
				set(val) {
					this.$store.commit('set_active_index', val);
				}
			}
		}
	};
	
	
</script>
<style type="text/css">
	.el-tabs--border-card>.el-tabs__content {
		padding: 0px;
	}
</style>
<style scoped>
	.main-container {
		height: 100%;
		width: 100%;
		box-sizing: border-box;
	}

	.main-aside-collapsed {
		/* 在CSS中,通过对某一样式声明! important ,可以更改默认的CSS样式优先级规则,使该条样式属性声明具有最高优先级 */
		width: 64px !important;
		height: 100%;
		background-color: #334157;
		margin: 0px;
	}

	.main-aside {
		width: 240px !important;
		height: 100%;
		background-color: #334157;
		margin: 0px;
	}

	.main-header,
	.main-center {
		padding: 0px;
		border-left: 2px solid #333;
	}
	
</style>

所需路由挂载index.js

import Vue from 'vue'
import Router from 'vue-router'
import login from '@/views/login'
import reg from '@/views/reg'
import AppMain from '@/components/AppMain'
import Articles from '@/views/sys/Articles'
import VuexPage1 from '@/views/sys/VuexPage1'
import VuexPage2 from '@/views/sys/VuexPage2'

Vue.use(Router) 

export default new Router({
	routes: [{
		path: '/',
		name: 'login',
		component: login
	},
	{
		path: '/login',
		name: 'login',
		component: login
	},
	{
		path: '/reg',
		name: 'reg',
		component: reg
	},
	{
		path: '/AppMain',
		name: 'AppMain',
		component: AppMain,
		children:[
			{
				path: '/sys/Articles',
				name: 'Articles',
				component: Articles
			},
			{	
				path: '/sys/VuexPage1',
				name: 'VuexPage1',
				component: VuexPage1
			},
			{
				path: '/sys/VuexPage2',
				name: 'VuexPage2',
				component: VuexPage2
			}
		]
	}]
})

组件挂载main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import 'element-ui/lib/theme-chalk/index.css'
// process.env.MOCK && require('@/mock')
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui' //引入
import VueAxios from 'vue-axios'
import axios from '@/api/http'


Vue.use(VueAxios, axios)
Vue.use(ElementUI)
Vue.config.productionTip = false

/* eslint-disable no-new */
window.vm = new Vue({
	el: '#app',
	data() {
		return {
			Bus: new Vue({

			})
		};
	},
	router,
	store,
	components: {
		App
	},
	template: '<App/>'
})

然后这个功能是基于上篇博客的代码实现的,有代码不懂的可以看一下SPA动态树和数据表格

end…

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页