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

<script>
13 14
import pathToRegexp from 'path-to-regexp'

Pan's avatar
Pan committed
15 16 17 18 19 20
export default {
  data() {
    return {
      levelList: null
    }
  },
Pan's avatar
Pan committed
21 22 23 24 25
  watch: {
    $route() {
      this.getBreadcrumb()
    }
  },
26 27 28
  created() {
    this.getBreadcrumb()
  },
Pan's avatar
Pan committed
29 30
  methods: {
    getBreadcrumb() {
花裤衩's avatar
花裤衩 committed
31 32
      // only show routes with meta.title
      let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
Pan's avatar
Pan committed
33
      const first = matched[0]
花裤衩's avatar
花裤衩 committed
34

yanzhongrong's avatar
yanzhongrong committed
35 36 37
      // if (!this.isDashboard(first)) {
      //   matched = [{ path: '/dashboard', meta: { title: '首页' }}].concat(matched)
      // }
38 39

      this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
40
    },
花裤衩's avatar
花裤衩 committed
41 42 43 44 45 46 47
    isDashboard(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
    },
48 49 50 51 52
    pathCompile(path) {
      // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
      const { params } = this.$route
      var toPath = pathToRegexp.compile(path)
      return toPath(params)
53 54 55 56 57 58 59 60
    },
    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        this.$router.push(redirect)
        return
      }
      this.$router.push(this.pathCompile(path))
Pan's avatar
Pan committed
61 62 63
    }
  }
}
Pan's avatar
Pan committed
64 65
</script>

花裤衩's avatar
花裤衩 committed
66 67 68 69 70
<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
yanzhongrong's avatar
yanzhongrong committed
71
  margin-left: 20px;
花裤衩's avatar
花裤衩 committed
72 73 74 75

  .no-redirect {
    color: #97a8be;
    cursor: text;
Pan's avatar
Pan committed
76
  }
花裤衩's avatar
花裤衩 committed
77
}
Pan's avatar
Pan committed
78
</style>