Blame view

HHECS.Web/src/components/Breadcrumb/index.vue 2.21 KB
胡菁 authored
1
2
3
<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
胡菁 authored
4
      <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
胡菁 authored
5
6
7
8
9
        <span
          v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
          class="no-redirect"
          >{{ $t(item.meta.title) }}</span
        >
胡菁 authored
10
11
12
        <a v-else @click.prevent="handleLink(item)">{{
          $t(item.meta.title)
        }}</a>
胡菁 authored
13
14
15
16
17
18
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
胡菁 authored
19
import pathToRegexp from "path-to-regexp";
20
import { getPermissionData } from "@/router/index";
胡菁 authored
21
22
23
export default {
  data() {
    return {
胡菁 authored
24
25
      levelList: null,
    };
胡菁 authored
26
27
28
  },
  watch: {
    $route() {
胡菁 authored
29
30
      this.getBreadcrumb();
    },
胡菁 authored
31
32
  },
  created() {
胡菁 authored
33
    this.getBreadcrumb();
胡菁 authored
34
35
36
37
  },
  methods: {
    getBreadcrumb() {
      // only show routes with meta.title
胡菁 authored
38
39
40
41
      let matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      );
      const first = matched[0];
胡菁 authored
42
胡菁 authored
43
      if (!this.isMain(first)) {
胡菁 authored
44
45
46
        matched = [
          { path: "/", meta: { title: this.$t("system.mainPage") } },
        ].concat(matched);
胡菁 authored
47
48
      }
胡菁 authored
49
50
51
      this.levelList = matched.filter(
        (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
      );
胡菁 authored
52
    },
胡菁 authored
53
54
    isMain(route) {
      const name = route && route.name;
胡菁 authored
55
      if (!name) {
胡菁 authored
56
        return false;
胡菁 authored
57
      }
胡菁 authored
58
      return name.trim().toLocaleLowerCase() === "main".toLocaleLowerCase();
胡菁 authored
59
60
61
    },
    pathCompile(path) {
      // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
胡菁 authored
62
63
64
      const { params } = this.$route;
      var toPath = pathToRegexp.compile(path);
      return toPath(params);
胡菁 authored
65
66
    },
    handleLink(item) {
67
68
69
70
71
72
73
74
      getPermissionData(() => {
        const { redirect, path } = item;
        if (redirect) {
          this.$router.push(redirect);
          return;
        }
        this.$router.push(this.pathCompile(path));
      });
胡菁 authored
75
76
77
    },
  },
};
胡菁 authored
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }
}
</style>