index.vue 1.97 KB
Newer Older
耿迪迪's avatar
耿迪迪 committed
1 2 3
<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
纪泽龙's avatar
纪泽龙 committed
4 5 6 7 8 9
      <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
        <span
          v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
          class="no-redirect"
          >{{ item.meta.title }}</span
        >
耿迪迪's avatar
耿迪迪 committed
10 11 12 13 14 15 16
        <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
纪泽龙's avatar
纪泽龙 committed
17 18
import store from "@/store";

耿迪迪's avatar
耿迪迪 committed
19 20 21
export default {
  data() {
    return {
纪泽龙's avatar
纪泽龙 committed
22 23
      levelList: null,
    };
耿迪迪's avatar
耿迪迪 committed
24 25 26 27
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
纪泽龙's avatar
纪泽龙 committed
28 29
      if (route.path.startsWith("/redirect/")) {
        return;
耿迪迪's avatar
耿迪迪 committed
30
      }
纪泽龙's avatar
纪泽龙 committed
31 32
      this.getBreadcrumb();
    },
耿迪迪's avatar
耿迪迪 committed
33 34
  },
  created() {
纪泽龙's avatar
纪泽龙 committed
35
    this.getBreadcrumb();
耿迪迪's avatar
耿迪迪 committed
36 37 38 39
  },
  methods: {
    getBreadcrumb() {
      // only show routes with meta.title
纪泽龙's avatar
纪泽龙 committed
40 41 42 43
      let matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      );
      const first = matched[0];
耿迪迪's avatar
耿迪迪 committed
44

纪泽龙's avatar
纪泽龙 committed
45 46 47 48
      if (
        !this.isDashboard(first) && store.getters.systemSetting.show_index === "1"
      ) {
        matched = [{ path: "/index", meta: { title: "首页" } }].concat(matched);
耿迪迪's avatar
耿迪迪 committed
49 50
      }

纪泽龙's avatar
纪泽龙 committed
51 52 53
      this.levelList = matched.filter(
        (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
      );
耿迪迪's avatar
耿迪迪 committed
54 55
    },
    isDashboard(route) {
纪泽龙's avatar
纪泽龙 committed
56
      const name = route && route.name;
耿迪迪's avatar
耿迪迪 committed
57
      if (!name) {
纪泽龙's avatar
纪泽龙 committed
58
        return false;
耿迪迪's avatar
耿迪迪 committed
59
      }
纪泽龙's avatar
纪泽龙 committed
60
      return name.trim() === "首页";
耿迪迪's avatar
耿迪迪 committed
61 62
    },
    handleLink(item) {
纪泽龙's avatar
纪泽龙 committed
63
      const { redirect, path } = item;
耿迪迪's avatar
耿迪迪 committed
64
      if (redirect) {
纪泽龙's avatar
纪泽龙 committed
65 66
        this.$router.push(redirect);
        return;
耿迪迪's avatar
耿迪迪 committed
67
      }
纪泽龙's avatar
纪泽龙 committed
68 69 70 71
      this.$router.push(path);
    },
  },
};
耿迪迪's avatar
耿迪迪 committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
</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>