网站首页菜单顶部下拉上下布局以及可关闭标签页实现vue+elementui

复制代码
<template>

  <div id="app">

    <el-menu
      :default-active="activeIndex2"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      background-color="#545c64"
      text-color="#fff"
      router
      active-text-color="#ffd04b">

      <el-submenu :index="item.path" :key="item.path" v-for="item in sidebarRouters" v-if="item.meta">
        <template slot="title" >{{ item.meta.title }}</template>
        <el-menu-item  :key="child.path" :index="item.path+'/'+child.path" v-for="child in item.children" v-if="child.meta" >
          {{ child.meta.title }}
        </el-menu-item>
      </el-submenu>

      <!--
      <el-menu-item index="/1" v-for="item of sidebarRouters">
        <span v-if="item.meta"> {{item.meta.title}} </span>
      </el-menu-item>
    <el-menu-item index="/bar" >消息中心</el-menu-item>
      <el-menu-item index="/foo" >消息中心2</el-menu-item>
      <el-menu-item index=""><a href="https://www.ele.me" target="_blank">订单管理</a></el-menu-item>-->
    </el-menu>

    <!-- 路由出口 -->
    <!-- 路由匹配到的组件将渲染在这里 -->
    <el-tag
      :key="tag"
      v-for="(tag ,index) in dynamicTags"
      closable
      :disable-transitions="false"
      :effect="tag.effect"
      @click="handleClick(tag)"
      @close="handleClose(tag)">
      <router-link :to="tag.path"> {{tag.name}} </router-link>
    </el-tag>

    <router-view></router-view>

  </div>

</template>

<script>

import {mapState} from "vuex";
import store from '@/store'
export default {
  name: 'Layout',
  data() {
    return {
      effectValue:'plain',
      dynamicTags: [{name:'首页',path:'/',effect:'plain'}],
      activeIndex2: '0',
      menuIndexName:{'/system/user':'用户管理', '/system/role':'角色管理',  '/system/menu':'菜单管理',  '/system/dept':'部门管理', '/system/dict':'字典管理', '/system/post':'岗位管理','/system/job':'任务管理', '/system/config':'参数管理', '/system/notice':'通知公告', '/system/logininfor':'登录日志', '/system/operlog':'操作日志'},
      sidebarRouters: store.getters.sidebarRouters
    };
  },
  methods: {
    handleSelect(key, keyPath) {
      //console.log(JSON.stringify(store.getters.sidebarRouters))
      console.log(key, keyPath);
      //置灰其它菜单按钮tag
      this.dynamicTags.forEach(item => {
        item.effect='plain';
      });
      //激活当前菜单按钮tag
      let menuName = this.menuIndexName[keyPath[1]];
      let menuNameJson={name:menuName, path:keyPath[1],effect:'dark'};
      this.dynamicTags.push(menuNameJson);

      //this.$router.push({path: keyPath});
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      //激活后一个按钮tag
      if(this.dynamicTags.length>1){
        let dynamicTagsCur = this.dynamicTags[this.dynamicTags.length-1];
        dynamicTagsCur.effect='dark';
        let returnUrl = dynamicTagsCur.path;
        //刷新路由
        //this.$router.replace(decodeURIComponent(returnUrl || '/'));
        this.$router.push({
          path: returnUrl,
          query: {
            ...this.$route.query,
            timestamp: Date.now()
          }
        });
      }
    },
    handleClick(tag){
      //置灰其它菜单按钮tag
      this.dynamicTags.forEach(item => {
        item.effect='plain';
      });
      //单击事件激活当前按钮
      tag.effect='dark';

    }
  }
};

</script>

<style scoped lang="scss">
.el-tag + .el-tag {
  margin: 5px;
}
</style>
相关推荐
爱健身的小刘同学24 分钟前
Vue 3 + Leaflet 地图可视化
前端·javascript·vue.js
神秘的猪头37 分钟前
Ajax 数据请求:从零开始掌握异步通信
前端·javascript
黛色正浓1 小时前
leetCode-热题100-贪心合集(JavaScript)
javascript·算法·leetcode
musashi1 小时前
用 Electron 写了一个 macOS 版本的 wallpaper(附源码、下载地址)
前端·vue.js·electron
徐徐子2 小时前
从vue3 watch开始理解Vue的响应式原理
前端·vue.js
拾荒的小海螺2 小时前
开源项目:Three.js 构建 3D 世界的工具库
javascript·3d·开源
还债大湿兄2 小时前
huggingface.co 下载有些要给权限的模型 小记录
开发语言·前端·javascript
叶落无痕522 小时前
Electron应用自动化测试实例
前端·javascript·功能测试·测试工具·electron·单元测试
H@Z*rTE|i2 小时前
elementUi 当有弹窗的时候提示语被覆盖的问题
前端·javascript·elementui
阿奇__2 小时前
vue2+elementUI table多个字段排序
前端·javascript·elementui