hejianhao
2025-04-24 c76e6648647e6174f4070313887d353a36e6d17c
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
<template>
  <!-- 判断当前页面是否显示,如果hide为true,则不渲染该菜单 -->
  <!-- <div v-if="!item.meta.hide && menus.includes(item.meta.privilege)"> -->
  <div v-if="item.meta && item.meta.title && !item.meta.hide">
    <!-- 根菜单 -->
    <MenuLink :to="resolvePath()" v-if="!item.children">
      <el-menu-item :index="resolvePath()">
        <i :class="(item.meta && item.meta.icon) || ''"></i>
        <span slot="title">{{ (item.meta && item.meta.title) || '' }}</span>
      </el-menu-item>
    </MenuLink>
 
    <!-- 可展开菜单 -->
    <el-submenu :index="resolvePath()" v-else>
      <template slot="title">
        <i :class="(item.meta && item.meta.icon) || ''"></i>
        <span slot="title">{{ (item.meta && item.meta.title) || '' }}</span>
      </template>
      <!-- 这里递归去展示多级菜单 -->
      <menu-item v-for="(route, index) in item.children" :key="index" :item="route"
        :fatherPath="resolvePath(route.path)">
      </menu-item>
    </el-submenu>
  </div>
</template>
 
<script>
// 引入path用来处理路径
import { mapState } from "vuex";
import path from "path";
import MenuLink from "./MenuLink.vue";
 
export default {
  // 做组件递归时必须定义一个name。然后递归时的组件名就是这里的name值
  name: "MenuItem",
  components: {
    MenuLink,
  },
  props: {
    // 上一级的路由信息
    item: {
      type: Object,
      default: null,
    },
    // 上一级的路径
    fatherPath: {
      type: String,
      default: "",
    },
  },
 
  computed: {
    ...mapState(["menus"]),
  },
  data() {
    return {};
  },
  methods: {
    // 判断路径是否是外部地址
    isExternal(path) {
      return /^(https?:|mailto:|tel:)/.test(path);
    },
    // 解析页面地址
    resolvePath(routePath = "") {
      // 判断当前页面地址是否为外部地址
      if (this.isExternal(routePath)) {
        return routePath;
      }
      // 判断从父组件传递过来的地址是否为外部地址
      if (this.isExternal(this.fatherPath)) {
        return this.fatherPath;
      }
      // 格式化页面地址
      return path.resolve(this.fatherPath, routePath);
    },
  },
};
</script>
<style lang="less" scoped>
::v-deep .router-link-exact-active .is-active {
  background: #EFF8FA;
  border-radius: 8px;
  font-weight: bold;
  color: #05908E;
}
 
::v-deep .el-menu-item,
::v-deep .el-submenu__title {
  border-radius: 8px;
  height: 40px;
  line-height: 40px;
}
 
::v-deep .el-menu-item:hover,
::v-deep .el-submenu__title:hover {
  background: #EFF8FA;
}
</style>