123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <template>
- <div class="Breadcrumb">
- <i
- class="el-icon-d-arrow-left fl"
- :class="{ cor: isShow }"
- @click="back_arrow"
- ></i>
- <el-breadcrumb class="app-breadcrumb" separator="/">
- <transition-group name="breadcrumb">
- <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
- <span
- v-if="item.redirect === 'noRedirect' || index > 0"
- class="no-redirect"
- >{{ item.meta.title }}</span
- >
- <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
- </el-breadcrumb-item>
- </transition-group>
- </el-breadcrumb>
- </div>
- </template>
- <script>
- import pathToRegexp from "path-to-regexp";
- export default {
- name: "Breadcrumb",
- data() {
- return {
- levelList: null,
- isShow: false,
- backOtions: null,
- backUrl: "",
- };
- },
- watch: {
- $route(route) {
- // if you go to the redirect page, do not update the breadcrumbs
- if (route.path.startsWith("/redirect/")) {
- this.isShow = false;
- return;
- }
- this.getBreadcrumb();
- },
- },
- created() {
- this.getBreadcrumb();
- },
- methods: {
- getBreadcrumb() {
- // only show routes with meta.title
- let matched = this.$route.matched.filter(
- (item) => item.meta && item.meta.title
- );
- const first = matched[0];
- if (!this.isDashboard(first)) {
- matched = [
- { path: "/welcome/dashboard", meta: { title: "首页" } },
- ].concat(matched);
- }
- this.levelList = matched.filter(
- (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
- );
- const { preModel } = this.$route.query;
- if (preModel) {
- let model = JSON.parse(preModel);
- this.isShow = true;
- const { options, router } = model;
- this.backOtions = options;
- this.backUrl = router;
- } else {
- this.isShow = false;
- }
- },
- isDashboard(route) {
- const name = route && route.name;
- if (!name) {
- return false;
- }
- return (
- name.trim().toLocaleLowerCase() === "Dashboard".toLocaleLowerCase()
- );
- },
- pathCompile(path) {
- const { params } = this.$route;
- var toPath = pathToRegexp.compile(path);
- return toPath(params);
- },
- back_arrow() {
- if (!this.isShow) {
- return;
- }
- window.vm.$router.push({
- path: this.backUrl,
- query: {
- back: JSON.stringify(this.backOtions),
- },
- });
- },
- handleLink(item) {
- const { redirect, path } = item;
- if (redirect) {
- window.vm.$router.push(redirect);
- return;
- }
- window.vm.$router.push(this.pathCompile(path));
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .Breadcrumb {
- .el-icon-d-arrow-left {
- display: inline-block;
- font-size: 14px;
- line-height: 50px;
- margin: 0 8px 0 1px;
- color: transparent;
- &.cor {
- color: #000;
- &:hover {
- cursor: pointer;
- }
- }
- }
- .app-breadcrumb.el-breadcrumb {
- display: inline-block;
- font-size: 14px;
- line-height: 50px;
- .no-redirect {
- color: #97a8be;
- cursor: text;
- }
- }
- }
- </style>
|