当前位置:   article > 正文

使用element-ui 做出导航菜单与Tabs 标签页联动,并实现Tabs标签的 重新加载、关闭所有、关闭其它等功能_element关闭tabs

element关闭tabs
使用element-ui 做出导航菜单与Tabs 标签页联动,并实现Tabs标签的 重新加载、关闭所有、关闭其它等功能


前言

这是我第一篇博客希望大家多多关注

提示:以下是本篇文章正文内容,下面案例可供参考

一、直接上代码

代码如下(示例):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context


<template>
  <div>
    <el-container>
      <el-header>
        <!-- 头部区域 -->
        <div class="Hui-header cl">
          <div class="logo-box">
            <div class="logo"></div>
            <div class="logo-r"></div>
          </div>
          <div class="robot"></div>
          <div class="Hui-welcome">
            <span>{{ DateTime | formDateFun(4) }}</span>&nbsp;
            <span>{{ Day[new Date().getDay()] }}</span>
            &nbsp;&nbsp;
            <span @click="customShow('修改密码', 'changPassword', 550, 395)">{{
              UserName
            }}</span>&nbsp;
            <div class="quit">
              <img src="../../assets/images/quit.png" />
              <a
                @click="exit"
                class="dropDown_A"
              >
                <font color="#ffffff">退出登录</font>
              </a>
            </div>
          </div>
        </div>
      </el-header>
      <el-container>
        <!-- 侧边栏菜单区域 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div
            class="toggle-button"
            @click="toggleCollapse"
          >|||</div>
          <!-- :router="true" -->
          <el-menu
            :collapse-transition="false"
            :collapse="isCollapse"
            :unique-opened="true"
            background-color="#fff"
            text-color="#545c64"
            active-text-color="#409EFF"
            :default-active="activationInedx"
            @select="handleSelect"
            :router="true"
          >
            <!-- 菜单一 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menu"
              :key="item.path"
            >
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.lable }}</span>
              </template>
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.path"
              >
                <template slot="title">
                  <i
                    class="el-icon-menu"
                    style="font-size: 15px"
                  ></i>
                  <span>{{ subItem.lable }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main class="main">
          <div v-show="contextMenuVisible">
            <ul :style="{left:left+'px',top:top+'px'}" class="contextmenu">
                <li><el-button type="text" @click="curTabReload()" size="mini">重新加载</el-button></li>
                <li><el-button type="text" @click="closeAllTabs()" size="mini">关闭所有</el-button></li>
                <li><el-button type="text" @click="closeOtherTabs('other')" size="mini">关闭其他</el-button></li>
            </ul>
          </div>
          <el-tabs
            v-model="editableTabsValue"
            type="border-card"
            closable
            @tab-remove="removeTab"
            @tab-click="tab_click(editableTabsValue)"
          >
            <el-tab-pane
              v-for="item in editableTabs"
              :key="item.name"
              :label="item.title"
              :name="item.name"
            >
              <!-- 路由站位符 -->
              <router-view></router-view>
            </el-tab-pane>
          </el-tabs>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style lang="less" scoped>
.contextmenu {
 	width: 100px;
    margin: 0;
    border: 1px solid #ccc;
    background: #fff;
    z-index: 3000;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 14px;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.2);
}
.contextmenu li {
    margin: 0;
    padding: 0px 22px;

}
.contextmenu li:hover {
    background: #f2f2f2;
    cursor: pointer;
}
.contextmenu li button{
   color: #2c3e50;
}
.el-tabs__nav .el-tabs__item:nth-child(1) span {
  display: none;
}

// .el-menu-item:nth-child(1) span {
//   display: none;
// }
.el-aside {
  margin-top: 25px;
  height: 100%;

  .el-menu {
    .el-menu-item {
      font-family: "微软雅黑";
      font-size: 13px;
      font-weight: normal;
    }

    border-right: none;
  }
}
.main {
  margin-top: 5px;
}
.toggle-button {
  background-color: #409eff;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
<script>
import "../../assets/css/skin.css";
import "../../assets/css/H-ui.min.css";
import { bus } from "../../bus.js";
export default {
  
  mounted: function () {
    // 获取dom
    let tab_top_dom = document.body.getElementsByClassName("el-tabs__header is-top")
    // 使用原生js 为单个dom绑定鼠标右击事件
    tab_top_dom[0].oncontextmenu = this.openContextMenu
    // 设置定时器
    const chatTimer = setInterval(() => {
      this.DateTime = Date.parse(new Date());
    }, 1000);
  },
  created() {
    bus.$on("goto", (url) => {
      if (url === "/login") {
        localStorage.clear();
      }
      this.$router.push(url);
    });
    bus.$on("msg", (msg) => {
      if (msg === "504") {
        this.$notify({
          title: "提示",
          message: "连接超时!",
        });
      }
    });
  },
  data() {
    return {
      left:'',
      top:'',
      //右键菜单的显示
      contextMenuVisible:false,
      //右键选中的tabs页签属性
      currentContextTab:'',
      
      flag: true,
      isCollapse: false,
      editableTabsValue: "welcome",
      editableTabs: [
        {
          title: "首页",
          name: "welcome",
          closable: false,
          content: "首页",
        },
      ],
      tabIndex: 2,

      //父菜单数组对象
      menu: [
        {
          id: "101",
          lable: "数据建模",
          children: [
            { id: 1, path: "WarehouseTree", lable: "隶属关系管理" },
            { id: 2, lable: "仓库建模管理", path: "Warehouse" },
            { id: 3, path: "ReservoirManagement", lable: "库区建模管理" },
            { id: 4, path: "ShelfManagement", lable: "货架建模管理" },
            { id: 5, path: "WarehouseLoc", lable: "货位建模管理" },
            { id: 5, path: "storeManagement", lable: "网关建模管理" },
            { id: 5, path: "MaterialCategory", lable: "货物类别管理" },
            { id: 5, path: "MaterialInfo", lable: "货物管理" },
            { id: 5, path: "Supplier", lable: "供应商管理" },
            { id: 5, path: "Customer", lable: "收物单位管理" },
            { id: 5, path: "Company", lable: "计量单位管理" },
            { id: 5, path: "UnitConversion", lable: "单位换算" },
            { id: 5, path: "StandardDescription", lable: "标准说明" },
          ],
        },
        {
          id: "102",
          lable: "仓库管理",
          children: [
            { id: 6, path: "StockManagement", lable: "库存管理" },
            { id: 7, path: "EnterWarehouse", lable: "入库管理" },
            { id: 8, path: "OutboundOrder", lable: "出库管理" },
            { id: 9, path: "countingTaskList", lable: "盘点管理" },
            { id: 10, path: "ScrapManagementList", lable: "报废管理" },
            { id: 10, path: "TransferTaskList", lable: "移库单管理" },
            { id: 10, path: "GeneratePurchaseList", lable: "采购单生成" },
          ],
        },
        {
          id: "103",
          lable: "查询管理",
          children: [
            { id: 1, path: "InformationService", lable: "货物查询" },
            { id: 1, path: "ShelfQuery", lable: "货架查询" },
            { id: 1, path: "IssueorReceiptQuery", lable: "出入库查询" },
            { id: 1, path: "InventoryQuery", lable: "盘点查询" },
            { id: 1, path: "InventoryAnalysis", lable: "库存分析" },
            { id: 1, path: "InventoryCheck", lable: "盘点分析" },
            { id: 1, path: "StockInAnalysis", lable: "出入库分析" },
            { id: 1, path: "OperationLog", lable: "操作日志" },
            { id: 1, path: "PrintManagement", lable: "打印管理" },
          ],
        },
        {
          id: "104",
          lable: "系统管理",
          children: [
            { id: 1, path: "UserManagement", lable: "用户管理" },
            { id: 1, path: "PrivilegeManagement", lable: "角色管理" },
            { id: 1, path: "SetTime", lable: "设置超时时间" },
          ],
        },
      ],
      iconsObj: {
        '101': "el-icon-coin",
        '102': "el-icon-house",
        '103': "el-icon-search",
        '104': "el-icon-setting",
      },
      //激活菜单项的index
      activationInedx: 0,

      HomeShow: window,
      UserName: window.localStorage.getItem("UserName"),
      DateTime: Date.parse(new Date()),
      Day: [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
      ],
    };
  },
  methods: {
    // 关闭其它标签页
    closeOtherTabs(par) {
      
        if (par == "other") {
            //TODO 关闭其他所有标签
           for (let i = 0; i < this.editableTabs.length; i++) {
             if(i!=0){
               if (this.editableTabs[i].name!=this.currentContextTab) {
                 console.log(this.editableTabs[i].name);
                 console.log(i);
               this.editableTabs.splice(i,1)
               console.log(this.editableTabs.length);
               i-=1
             }
             }
             
             
           }
           this.editableTabsValue=this.currentContextTab
           this.$router.push("/" + this.currentContextTab);
           
        }
        //关闭菜单栏
        this.closeContextMenu()
    },
    
    //刷新当前页
    curTabReload(){
      this.$router.push("/" + this.currentContextTab);
      //TODO 重新载入当前标签页
      //关闭菜单栏
        this.closeContextMenu()
    },
    // 关闭所有标签页
    closeAllTabs() {
        //TODO 关闭所有标签
        // this.editableTabs
        console.log(this.editableTabs.length);
        this.editableTabs.splice(1,this.editableTabs.length-1)
        
       this.editableTabsValue='welcome'
       this.$router.push("/" + "welcome");
        //关闭菜单栏
        this.closeContextMenu()
        
    },
    // 关闭contextMenu
    closeContextMenu() {
      this.contextMenuVisible = false;
      
    },
        /*
    右击事件
      */
    openContextMenu(e) {
        e.preventDefault(); //防止默认菜单弹出
        
        let obj = e.srcElement ? e.srcElement : e.target;
        console.log('右击');
        console.log(obj);

        if (obj.id) {
            let currentContextTabId = obj.id.split("-")[1];
            this.currentContextTab=currentContextTabId
            let curIndex = 0;
            for(;curIndex<this.editableTabs.length;++curIndex){
                if(this.editableTabs[curIndex].title == currentContextTabId){
                    break;
                }
            }
            if(curIndex<=0){
                this.isDisabledCloseLeftBtnFlag = true
                this.isDisabledCloseRightBtnFlag = false
            }else if(curIndex >= this.editableTabs.length-1){
                this.isDisabledCloseLeftBtnFlag = false
                this.isDisabledCloseRightBtnFlag = true
            }else{
                this.isDisabledCloseLeftBtnFlag = false
                this.isDisabledCloseRightBtnFlag = false
            }
            this.contextMenuVisible = true;
            this.$store.commit("saveCurContextTabId", currentContextTabId);
            this.left = e.clientX;
            this.top = e.clientY + 10;
        }
    },
    //折叠菜单栏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },

    customShow(title, url, w, h, id) {
      layer_show(title, url + "?res=" + id, w, h);
    },
    //退出
    exit() {
      let that = this;
      let params = new FormData();
      localStorage.clear();
      that.$notify({
        title: "提示",
        message: "退出成功!",
      });
      console.log("======================456");
      that.$router.push("/login");
    },

    //选中tab菜单触发事件
    tab_click(index) {
      this.$router.push("/" + index);

      // 对子菜单的联动
      this.activationInedx = "/" + index;
    },
    //点击菜单项触发事件
    handleSelect(key, keyPath) {
      const keys = key.split("/")[1];
      for (let s = 0; s < this.editableTabs.length; s++) {
        this.editableTabs.map((tabs) => {
          if (tabs.name == keys) {
            this.editableTabsValue = keys;
            this.activationInedx = "/" + keys;
            this.flag = false;
          }
        });
      }
      if (this.flag) {
        this.menu.map((item) => {
          const itemList = item.children;
          itemList.map((res) => {
            if (res.path == keys) {
              console.log(res.lable);
              // key 代表访问路径
              // 实现tab菜单生成
              this.editableTabs.push({
                title: res.lable,
                name: keys,
                content: res.lable,
              });
              this.editableTabsValue = keys;
              this.activationInedx = "/" + keys;
            }
          });
        });
      }
      this.flag = true;
    },
    removeTab(targetName) {
      let tabs = this.editableTabs;
      let activeName = this.editableTabsValue;
      if (targetName === "welcome") {
        return;
      }
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            let nextTab = tabs[index + 1] || tabs[index - 1];
            if (nextTab) {
              activeName = nextTab.name;
            }
          }
        });
      }

      this.editableTabsValue = activeName;
      this.editableTabs = tabs.filter((tab) => tab.name !== targetName);
      this.$router.push("/" + activeName);
    },
  },
};
</script>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486

该处使用的url网络请求的数据。


总结

有不明白的可以加博主qq:2538720288 ,希望能帮助你们。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/530517
推荐阅读
相关标签
  

闽ICP备14008679号