Blame view

yudao-ui-admin/src/components/TopNav/index.vue 5.51 KB
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
1
2
  <template>
    <el-menu
d86c36a1   xingyu   vue2 圆角标签栏
3
4
5
      :default-active="activeMenu"
      mode="horizontal"
      @select="handleSelect"
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
6
7
    >
      <template v-for="(item, index) in topMenus">
d86c36a1   xingyu   vue2 圆角标签栏
8
9
10
11
        <el-menu-item :style="{'--theme': theme}" :index="item.path" :key="index" v-if="index < visibleNumber">
          <svg-icon :icon-class="item.meta.icon"/>
          {{ item.meta.title }}
        </el-menu-item>
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
12
13
14
      </template>
  
      <!-- 顶部菜单超出数量折叠 -->
f1f602c1   YunaiV   v3.7.0 顶部菜单样式调整
15
      <el-submenu :style="{'--theme': theme}" index="more" v-if="topMenus.length > visibleNumber">
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
16
17
18
        <template slot="title">更多菜单</template>
        <template v-for="(item, index) in topMenus">
          <el-menu-item
d86c36a1   xingyu   vue2 圆角标签栏
19
20
21
            :index="item.path"
            :key="index"
            v-if="index >= visibleNumber"
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
22
          >
d86c36a1   xingyu   vue2 圆角标签栏
23
24
25
            <svg-icon :icon-class="item.meta.icon"/>
            {{ item.meta.title }}
          </el-menu-item>
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
26
27
28
29
30
31
32
33
        </template>
      </el-submenu>
    </el-menu>
  </template>
  
  <script>
  import { constantRoutes } from "@/router";
  
54059564   YunaiV   v3.8.2 topNav自定义隐...
34
35
36
  // 隐藏侧边栏路由
  const hideList = ['/index', '/user/profile'];
  
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
37
38
39
40
41
  export default {
    data() {
      return {
        // 顶部栏初始数
        visibleNumber: 5,
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
42
43
        // 当前激活菜单的 index
        currentIndex: undefined
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
44
45
46
      };
    },
    computed: {
62cc1206   YunaiV   v3.5.0 主题颜色保存配置
47
48
49
      theme() {
        return this.$store.state.settings.theme;
      },
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
50
51
      // 顶部显示菜单
      topMenus() {
cff4391f   YunaiV   v3.5.0 布局设置支持保存&重置配置
52
53
        let topMenus = [];
        this.routers.map((menu) => {
63b7ee09   YunaiV   v3.5.0 固定顶部导航栏&窗口...
54
          if (menu.hidden !== true) {
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
55
56
57
58
59
60
            // 兼容顶部栏一级菜单内部跳转
            if (menu.path === "/") {
              topMenus.push(menu.children[0]);
            } else {
              topMenus.push(menu);
            }
cff4391f   YunaiV   v3.5.0 布局设置支持保存&重置配置
61
62
63
          }
        });
        return topMenus;
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
64
65
66
67
68
69
70
      },
      // 所有的路由信息
      routers() {
        return this.$store.state.permission.topbarRouters;
      },
      // 设置子路由
      childrenMenus() {
3cee922e   YunaiV   优化 permission 相关的注释
71
        const childrenMenus = [];
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
72
        this.routers.map((router) => {
011dc236   xingyu4j   替换var 为const/let
73
          for (let item in router.children) {
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
74
            if (router.children[item].parentPath === undefined) {
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
75
              if(router.path === "/") {
1a2c03bc   YunaiV   v3.8.2 开启TopNav没有...
76
                router.children[item].path = "/" + router.children[item].path;
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
77
              } else {
471175b4   YunaiV   v3.5.0 修复开启TopNav...
78
79
80
                if(!this.ishttp(router.children[item].path)) {
                  router.children[item].path = router.path + "/" + router.children[item].path;
                }
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
81
              }
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
82
83
84
85
86
87
88
89
90
91
              router.children[item].parentPath = router.path;
            }
            childrenMenus.push(router.children[item]);
          }
        });
        return constantRoutes.concat(childrenMenus);
      },
      // 默认激活的菜单
      activeMenu() {
        const path = this.$route.path;
1a2c03bc   YunaiV   v3.8.2 开启TopNav没有...
92
        let activePath = path;
54059564   YunaiV   v3.8.2 topNav自定义隐...
93
        if (path !== undefined && path.lastIndexOf("/") > 0 && hideList.indexOf(path) === -1) {
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
94
95
          const tmpPath = path.substring(1, path.length);
          activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
d86c36a1   xingyu   vue2 圆角标签栏
96
97
98
          if (!this.$route.meta.link) {
            this.$store.dispatch('app/toggleSideBarHide', false)
          }
1a2c03bc   YunaiV   v3.8.2 开启TopNav没有...
99
100
101
        } else if(!this.$route.children) {
          activePath = path;
          this.$store.dispatch('app/toggleSideBarHide', true);
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
102
        }
1a2c03bc   YunaiV   v3.8.2 开启TopNav没有...
103
        this.activeRoutes(activePath);
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
104
105
106
        return activePath;
      },
    },
63b7ee09   YunaiV   v3.5.0 固定顶部导航栏&窗口...
107
108
109
110
111
112
    beforeMount() {
      window.addEventListener('resize', this.setVisibleNumber)
    },
    beforeDestroy() {
      window.removeEventListener('resize', this.setVisibleNumber)
    },
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
113
114
115
116
117
118
    mounted() {
      this.setVisibleNumber();
    },
    methods: {
      // 根据宽度计算设置显示栏数
      setVisibleNumber() {
63b7ee09   YunaiV   v3.5.0 固定顶部导航栏&窗口...
119
120
        const width = document.body.getBoundingClientRect().width / 3;
        this.visibleNumber = parseInt(width / 85);
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
121
      },
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
122
123
      // 菜单选择事件
      handleSelect(key, keyPath) {
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
124
        this.currentIndex = key;
1a2c03bc   YunaiV   v3.8.2 开启TopNav没有...
125
        const route = this.routers.find(item => item.path === key);
471175b4   YunaiV   v3.5.0 修复开启TopNav...
126
        if (this.ishttp(key)) {
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
127
128
          // http(s):// 路径新窗口打开
          window.open(key, "_blank");
1a2c03bc   YunaiV   v3.8.2 开启TopNav没有...
129
130
131
132
        } else if (!route || !route.children) {
          // 没有子路由路径内部打开
          this.$router.push({ path: key });
          this.$store.dispatch('app/toggleSideBarHide', true);
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
133
        } else {
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
134
          // 显示左侧联动菜单
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
135
          this.activeRoutes(key);
9b47dd4a   YunaiV   b4bdd4f:开启TopNav没...
136
137
138
          if (!this.$route.meta.link) {
            this.$store.dispatch('app/toggleSideBarHide', false);
          }
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
139
140
141
142
        }
      },
      // 当前激活的路由
      activeRoutes(key) {
011dc236   xingyu4j   替换var 为const/let
143
        const routes = []
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
144
145
        if (this.childrenMenus && this.childrenMenus.length > 0) {
          this.childrenMenus.map((item) => {
011dc236   xingyu4j   替换var 为const/let
146
            if (key === item.parentPath || (key === "index" && "" === item.path)) {
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
147
148
149
150
              routes.push(item);
            }
          });
        }
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
151
152
        if(routes.length > 0) {
          this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
d86c36a1   xingyu   vue2 圆角标签栏
153
        } else {
9b47dd4a   YunaiV   b4bdd4f:开启TopNav没...
154
          this.$store.dispatch('app/toggleSideBarHide', true);
41c6aa91   YunaiV   v3.5.0 兼容顶部栏一级菜单内部跳转
155
        }
471175b4   YunaiV   v3.5.0 修复开启TopNav...
156
157
158
      },
      ishttp(url) {
        return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
cff4391f   YunaiV   v3.5.0 布局设置支持保存&重置配置
159
      }
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
160
161
162
163
    },
  };
  </script>
  
62cc1206   YunaiV   v3.5.0 主题颜色保存配置
164
  <style lang="scss">
f1f602c1   YunaiV   v3.7.0 顶部菜单样式调整
165
  .topmenu-container.el-menu--horizontal > .el-menu-item {
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
166
    float: left;
f1f602c1   YunaiV   v3.7.0 顶部菜单样式调整
167
168
169
170
171
    height: 50px !important;
    line-height: 50px !important;
    color: #999093 !important;
    padding: 0 5px !important;
    margin: 0 10px !important;
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
172
173
  }
  
f1f602c1   YunaiV   v3.7.0 顶部菜单样式调整
174
175
  .topmenu-container.el-menu--horizontal > .el-menu-item.is-active, .el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
    border-bottom: 2px solid #{'var(--theme)'} !important;
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
176
177
    color: #303133;
  }
62cc1206   YunaiV   v3.5.0 主题颜色保存配置
178
179
  
  /* submenu item */
f1f602c1   YunaiV   v3.7.0 顶部菜单样式调整
180
181
  .topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title {
    float: left;
62cc1206   YunaiV   v3.5.0 主题颜色保存配置
182
183
    height: 50px !important;
    line-height: 50px !important;
f1f602c1   YunaiV   v3.7.0 顶部菜单样式调整
184
185
186
    color: #999093 !important;
    padding: 0 5px !important;
    margin: 0 10px !important;
62cc1206   YunaiV   v3.5.0 主题颜色保存配置
187
  }
4bf5b04d   YunaiV   v3.5.0 新增菜单导航显示风格...
188
  </style>