作者 朱兆平

界面增加tab处理

  1 +<template>
  2 + <el-tabs v-model="activeIndex"
  3 + v-if="openTab.length"
  4 + type="card"
  5 + :closable = "tabCloseable"
  6 + @tab-click='tabClick'
  7 + @tab-remove="tabRemove"
  8 + style="width: 100%">
  9 + <el-tab-pane
  10 + v-for="(item, index) in this.$store.state.openTab"
  11 + :key="item.name"
  12 + :label="item.name"
  13 + :name="item.route"
  14 + >
  15 +
  16 + <section class="content-container">
  17 + <div class="grid-content bg-purple-light">
  18 + <!-- <el-col :span="24" class="breadcrumb-container">-->
  19 + <!-- <strong class="title">{{$route.name}}</strong>-->
  20 + <!-- <el-breadcrumb separator="/" class="breadcrumb-inner">-->
  21 + <!-- <el-breadcrumb-item v-for="item in $route.matched" :key="item.path">-->
  22 + <!-- {{ item.name }}-->
  23 + <!-- </el-breadcrumb-item>-->
  24 + <!-- </el-breadcrumb>-->
  25 + <!-- </el-col>-->
  26 + <el-col :span="24" class="content-wrapper">
  27 + <transition name="fade" mode="out-in">
  28 + <router-view></router-view>
  29 + </transition>
  30 + </el-col>
  31 + </div>
  32 + </section>
  33 + </el-tab-pane>
  34 + </el-tabs>
  35 +</template>
  36 +
  37 +<script>
  38 + export default {
  39 + name: 'TabMenu',
  40 + data() {
  41 + return {
  42 + tabCloseable: true
  43 + }
  44 + },
  45 + methods: {
  46 + initTab(){
  47 + // 刷新时以当前路由做为tab加入tabs
  48 + // 当前路由不是首页时,添加首页以及另一页到store里,并设置激活状态
  49 + // 当当前路由是首页时,添加首页到store,并设置激活状态
  50 + if (this.$route.path !== '/' && this.$route.path !== '/main') {
  51 + // console.log('1');
  52 + this.$store.commit('add_tabs', {route: '/main' , name: '首页'});
  53 + this.$store.commit('add_tabs', {route: this.$route.path , name: this.$route.name });
  54 + this.$store.commit('set_active_index', this.$route.path);
  55 + } else {
  56 + // console.log('2');
  57 + this.$store.commit('add_tabs', {route: '/main', name: '首页'});
  58 + this.$store.commit('set_active_index', '/main');
  59 + this.$router.push('/main');
  60 + }
  61 + },
  62 + tabClick(tab){
  63 + // console.log("tab",tab);
  64 + // console.log('active',this.$store.state.activeIndex);
  65 + this.$router.push({path: this.$store.state.activeIndex});
  66 + },
  67 + tabRemove(targetName){
  68 + // console.log("tabRemove",targetName);
  69 + //首页不删
  70 + if(targetName == '/main'){
  71 + return
  72 + }
  73 + this.$store.commit('delete_tabs', targetName);
  74 + if (this.$store.state.activeIndex === targetName) {
  75 + // 设置当前激活的路由
  76 + if (this.$store.state.openTab && this.$store.state.openTab.length >=1) {
  77 + // console.log('=============',this.$store.state.openTab[this.$store.state.openTab.length-1].route)
  78 + this.$store.commit('set_active_index', this.$store.state.openTab[this.$store.state.openTab.length-1].route);
  79 + this.$router.push({path: this.$store.state.activeIndex});
  80 + this.tabCloseable = false;
  81 + } else {
  82 + this.$router.push({path: '/main'});
  83 + }
  84 + }
  85 + },
  86 + },
  87 + mounted() {
  88 + this.initTab();
  89 + },
  90 + watch:{
  91 + '$route'(to,from){
  92 + //判断路由是否已经打开
  93 + //已经打开的 ,将其置为active
  94 + //未打开的,将其放入队列里
  95 + let flag = false;
  96 + for(let item of this.$store.state.openTab){
  97 + // console.log("item.name",item.name)
  98 + // console.log("t0.name",to.name)
  99 +
  100 + if(item.name === to.name){
  101 + // console.log('to.path',to.path);
  102 + this.$store.commit('set_active_index',to.path)
  103 + flag = true;
  104 + break;
  105 + }
  106 + }
  107 +
  108 + if(!flag){
  109 + // console.log('to.path',to.path);
  110 + this.$store.commit('add_tabs', {route: to.path, name: to.name});
  111 + this.$store.commit('set_active_index', to.path);
  112 + }
  113 +
  114 + }
  115 + },
  116 + computed:{
  117 + openTab:{
  118 + get: function () {
  119 + return this.$store.state.openTab;
  120 + },
  121 + set:function (value) {
  122 + this.$store.commit('set_tabs', {value});
  123 + // console.log("opebTab监视:value = "+ value);
  124 + }
  125 +
  126 + },
  127 + activeIndex:{
  128 + get:function () {
  129 + return this.$store.state.activeIndex;
  130 + },
  131 + set:function (value) {
  132 + this.$store.commit('set_active_index', value);
  133 + }
  134 +
  135 + }
  136 + }
  137 + }
  138 +</script>
  139 +
  140 +<style>
  141 +</style>
@@ -16,6 +16,7 @@ import 'font-awesome/css/font-awesome.min.css' @@ -16,6 +16,7 @@ import 'font-awesome/css/font-awesome.min.css'
16 import * as socketApi from './api/socket' 16 import * as socketApi from './api/socket'
17 import '@/styles/index.scss' 17 import '@/styles/index.scss'
18 import sys_init from '@/common/init/sys_init' 18 import sys_init from '@/common/init/sys_init'
  19 +import jsutil from "@/common/js/util";
19 20
20 21
21 //定义一个全局过滤器实现日期格式化 22 //定义一个全局过滤器实现日期格式化
@@ -30,7 +31,7 @@ Vue.prototype.socketApi = socketApi @@ -30,7 +31,7 @@ Vue.prototype.socketApi = socketApi
30 31
31 Vue.config.productionTip = false 32 Vue.config.productionTip = false
32 Mock.bootstrap(); 33 Mock.bootstrap();
33 -Vue.use(ElementUI) 34 +Vue.use(ElementUI, {size: 'mini'})
34 Vue.use(VueRouter) 35 Vue.use(VueRouter)
35 Vue.use(Vuex) 36 Vue.use(Vuex)
36 Vue.prototype.$axios = axios; 37 Vue.prototype.$axios = axios;
@@ -44,7 +45,8 @@ const router = new VueRouter({ @@ -44,7 +45,8 @@ const router = new VueRouter({
44 Vue.prototype.$rout =router; 45 Vue.prototype.$rout =router;
45 46
46 //本地存储系统初始化的router路由,用来处理用户退出后,把路由数据初始化 47 //本地存储系统初始化的router路由,用来处理用户退出后,把路由数据初始化
47 -sessionStorage.setItem('sysMenu', JSON.stringify(rout.routes)); 48 +// sessionStorage.setItem('sysMenu', JSON.stringify(rout.routes));
  49 +
48 50
49 const message = Vue.prototype.$message; 51 const message = Vue.prototype.$message;
50 sys_init.init_axios(message,router,axios); 52 sys_init.init_axios(message,router,axios);
1 import Login from './views/Login.vue' 1 import Login from './views/Login.vue'
2 import NotFound from './views/404.vue' 2 import NotFound from './views/404.vue'
3 import Home from './views/Home.vue' 3 import Home from './views/Home.vue'
4 -import Main from './views/dashboard/index' 4 +import Main from './views/Main.vue'
  5 +import Dashboard from './views/dashboard/index'
5 // import Table from './views/nav1/Table.vue' 6 // import Table from './views/nav1/Table.vue'
6 import Role from './views/nav1/role.vue' 7 import Role from './views/nav1/role.vue'
7 import Perm from './views/nav1/perm.vue' 8 import Perm from './views/nav1/perm.vue'
@@ -90,7 +91,6 @@ import ElasticSearchInfo from "./views/bus/ElasticSearchInfo.vue" @@ -90,7 +91,6 @@ import ElasticSearchInfo from "./views/bus/ElasticSearchInfo.vue"
90 import RouterBatch from "./views/bus/RouterBatch.vue" 91 import RouterBatch from "./views/bus/RouterBatch.vue"
91 import MessageManagement from "./views/bus/MessageManagement.vue" 92 import MessageManagement from "./views/bus/MessageManagement.vue"
92 import RouterManage from "./views/bus/RouterManage" 93 import RouterManage from "./views/bus/RouterManage"
93 -  
94 //组件练习 94 //组件练习
95 //import Page7 from "./views/nav3/Page7.vue" 95 //import Page7 from "./views/nav3/Page7.vue"
96 96
@@ -112,12 +112,14 @@ let routes = [ @@ -112,12 +112,14 @@ let routes = [
112 }, 112 },
113 { 113 {
114 path: '/', 114 path: '/',
115 - redirect: '/login', 115 + name: "主页",
  116 + redirect: '/main',
  117 + hidden: true
116 }, 118 },
117 { 119 {
118 path: '/', 120 path: '/',
119 component: Home, 121 component: Home,
120 - name: '主页', 122 + name: 'INDEX',
121 leaf: true, 123 leaf: true,
122 iconCls: 'el-icon-menu', 124 iconCls: 'el-icon-menu',
123 children: [ 125 children: [
@@ -135,21 +137,21 @@ let routes = [ @@ -135,21 +137,21 @@ let routes = [
135 // ] 137 // ]
136 // }, 138 // },
137 // { path: '/test', component: Main }, 139 // { path: '/test', component: Main },
138 - {  
139 - path: '/admin',  
140 - component: Home,  
141 - name: '系统设置',  
142 - iconCls: 'el-icon-setting',//图标样式class  
143 - children: [  
144 - {path: '/user', component: User, name: '用户管理'},  
145 - {path: '/role', component: Role, name: '组织机构'},  
146 - {path: '/perm', component: Perm, name: '权限管理'},  
147 - {path: '/log', component: LOG, name: '系统日志'},  
148 - {path: '/department', component: Department, name: '部门管理'},  
149 - {path: '/company', component: Company, name: '公司管理'},  
150 - {path: '/group', component: Group, name: '集团管理'}  
151 - ]  
152 - }, 140 + // {
  141 + // path: '/admin',
  142 + // component: Home,
  143 + // name: '系统设置',
  144 + // iconCls: 'el-icon-setting',//图标样式class
  145 + // children: [
  146 + // {path: '/user', component: User, name: '用户管理'},
  147 + // {path: '/role', component: Role, name: '组织机构'},
  148 + // {path: '/perm', component: Perm, name: '权限管理'},
  149 + // {path: '/log', component: LOG, name: '系统日志'},
  150 + // {path: '/department', component: Department, name: '部门管理'},
  151 + // {path: '/company', component: Company, name: '公司管理'},
  152 + // {path: '/group', component: Group, name: '集团管理'}
  153 + // ]
  154 + // },
153 155
154 { 156 {
155 path: '/bus', 157 path: '/bus',
@@ -157,7 +159,7 @@ let routes = [ @@ -157,7 +159,7 @@ let routes = [
157 name: '消息控制中心', 159 name: '消息控制中心',
158 iconCls: 'el-icon-message', 160 iconCls: 'el-icon-message',
159 children: [ 161 children: [
160 - {path: '/userInfo', component: UserInfo, name: '用户关系管理'}, 162 + // {path: '/userInfo', component: UserInfo, name: '用户关系管理'},
161 {path: '/server', component: Server, name: '服务器管理'}, 163 {path: '/server', component: Server, name: '服务器管理'},
162 // {path: '/host', component: VirtualHost, name: '虚拟主机管理'}, 164 // {path: '/host', component: VirtualHost, name: '虚拟主机管理'},
163 {path: '/queue', component: Queue, name: '队列管理'}, 165 {path: '/queue', component: Queue, name: '队列管理'},
@@ -183,15 +185,6 @@ let routes = [ @@ -183,15 +185,6 @@ let routes = [
183 // {path: '/exchangeView', component: ExchangeView, name: '交换机监控'}, 185 // {path: '/exchangeView', component: ExchangeView, name: '交换机监控'},
184 ] 186 ]
185 }, 187 },
186 - {  
187 - path: '/nav3',  
188 - component: Home,  
189 - name: '组件练习',  
190 - iconCls: 'el-icon-view',  
191 - children: [  
192 - {path: '/page7', component: Page7, name: '组件'},  
193 - ]  
194 - },  
195 188
196 189
197 // { 190 // {
@@ -86,23 +86,9 @@ @@ -86,23 +86,9 @@
86 </li> 86 </li>
87 </ul> 87 </ul>
88 </aside> 88 </aside>
89 -<section class="content-container">  
90 - <div class="grid-content bg-purple-light">  
91 - <el-col :span="24" class="breadcrumb-container">  
92 - <strong class="title">{{$route.name}}</strong>  
93 - <el-breadcrumb separator="/" class="breadcrumb-inner">  
94 - <el-breadcrumb-item v-for="item in $route.matched" :key="item.path">  
95 - {{ item.name }}  
96 - </el-breadcrumb-item>  
97 - </el-breadcrumb>  
98 - </el-col>  
99 - <el-col :span="24" class="content-wrapper">  
100 - <transition name="fade" mode="out-in">  
101 - <router-view :key="$route.path +$route.query.t"></router-view>  
102 - </transition>  
103 - </el-col>  
104 - </div>  
105 -</section> 89 +
  90 +<TabMenu></TabMenu>
  91 +
106 </el-col> 92 </el-col>
107 </el-row> 93 </el-row>
108 </template> 94 </template>
@@ -111,6 +97,7 @@ @@ -111,6 +97,7 @@
111 import rt from '../routes' 97 import rt from '../routes'
112 import {editPass, resetToken} from '../api/user'; 98 import {editPass, resetToken} from '../api/user';
113 import ElFormItem from "element-ui/packages/form/src/form-item"; 99 import ElFormItem from "element-ui/packages/form/src/form-item";
  100 + import TabMenu from "@/components/TabMenu"
114 101
115 export default { 102 export default {
116 103
@@ -119,7 +106,7 @@ @@ -119,7 +106,7 @@
119 reload: this.reload 106 reload: this.reload
120 } 107 }
121 }, 108 },
122 - components: {ElFormItem}, 109 + components: {ElFormItem,TabMenu},
123 data() { 110 data() {
124 var validatePass = (rule, value, callback) => { 111 var validatePass = (rule, value, callback) => {
125 if (!value) { 112 if (!value) {
@@ -219,14 +206,15 @@ @@ -219,14 +206,15 @@
219 }, 206 },
220 methods: { 207 methods: {
221 reload() { 208 reload() {
222 - this.$nextTick(function () {  
223 - this.$router.push({  
224 - path: this.$router.path,  
225 - query: {  
226 - t: new Date().getTime()  
227 - }  
228 - })  
229 - }) 209 + // this.$nextTick(function () {
  210 + // this.$router.push({
  211 + // path: this.$router.path,
  212 + // query: {
  213 + // t: new Date().getTime()
  214 + // }
  215 + // })
  216 + // })
  217 + // this.addTab(this.$router);
230 }, 218 },
231 editPass: function () { 219 editPass: function () {
232 this.dialogFormVisible = true; 220 this.dialogFormVisible = true;
@@ -353,8 +341,8 @@ @@ -353,8 +341,8 @@
353 if (userRouters) { 341 if (userRouters) {
354 userRouters = JSON.parse(userRouters); 342 userRouters = JSON.parse(userRouters);
355 _this.$router.options.routes = userRouters; 343 _this.$router.options.routes = userRouters;
356 - console.log("home:");  
357 - console.log(_this.$router.options.routes); 344 + // console.log("home:");
  345 + // console.log(_this.$router.options.routes);
358 } 346 }
359 } 347 }
360 } 348 }
@@ -11,6 +11,9 @@ const state = { @@ -11,6 +11,9 @@ const state = {
11 count: 10, 11 count: 10,
12 serverList: [], 12 serverList: [],
13 virtualHostList: [], 13 virtualHostList: [],
  14 + openTab:[],//所有打开的路由
  15 + activeIndex: '/main' //激活状态
  16 +
14 } 17 }
15 18
16 // 定义所需的 mutations 19 // 定义所需的 mutations
@@ -38,6 +41,28 @@ const mutations = { @@ -38,6 +41,28 @@ const mutations = {
38 this.$message.error(error.toString()); 41 this.$message.error(error.toString());
39 }); 42 });
40 }, 43 },
  44 + // 添加tabs
  45 + add_tabs (state, data) {
  46 + this.state.openTab.push(data);
  47 + },
  48 + set_tabs(state,value){
  49 + this.state.openTab= value;
  50 + },
  51 + // 删除tabs
  52 + delete_tabs (state, route) {
  53 + let index = 0;
  54 + for (let option of state.openTab) {
  55 + if (option.route === route) {
  56 + break;
  57 + }
  58 + index++;
  59 + }
  60 + this.state.openTab.splice(index, 1);
  61 + },
  62 + // 设置当前激活的tab
  63 + set_active_index (state, index) {
  64 + this.state.activeIndex = index;
  65 + },
41 66
42 67
43 INCREMENT(state) { 68 INCREMENT(state) {