|
| 1 | +<!-- |
| 2 | + * @Descripttion: |
| 3 | + * @version: |
| 4 | + * @Author: wenlan |
| 5 | + * @Date: 2022-02-13 14:50:20 |
| 6 | + * @LastEditors: wenlan |
| 7 | + * @LastEditTime: 2022-02-13 17:39:27 |
| 8 | +--> |
| 9 | +<script setup lang="ts"> |
| 10 | +import { compile } from 'path-to-regexp' |
| 11 | +import { toRefs, reactive, watch } from 'vue' |
| 12 | +import { useRoute, useRouter, RouteRecordRaw } from 'vue-router' |
| 13 | +import type { IBreadcrumbList } from '../type' |
| 14 | +const route = useRoute() |
| 15 | +const router = useRouter() |
| 16 | +let breadList = reactive<IBreadcrumbList>({ |
| 17 | + list: [] |
| 18 | +}) |
| 19 | +const { list } = toRefs(breadList) |
| 20 | +watch( |
| 21 | + () => route.name, |
| 22 | + (newValue) => { |
| 23 | + console.log('@', newValue) |
| 24 | + getBreadcrumb() |
| 25 | + }, |
| 26 | + { deep: true } |
| 27 | +) |
| 28 | +const pathCompile = (path: string) => { |
| 29 | + const { params } = route |
| 30 | + const toPath = compile(path) //解决url中携带参数问题 |
| 31 | + return toPath(params) |
| 32 | +} |
| 33 | +const isDashboard = (route: RouteRecordRaw) => { |
| 34 | + const name = route?.name |
| 35 | + if (!name) { |
| 36 | + return false |
| 37 | + } |
| 38 | + return ( |
| 39 | + (name as string).trim().toLocaleLowerCase() === |
| 40 | + 'dashboard'.toLocaleLowerCase() |
| 41 | + ) |
| 42 | +} |
| 43 | +const getBreadcrumb = () => { |
| 44 | + // only show routes with meta.title |
| 45 | + let matched = route.matched.filter((item) => item.meta && item.meta.title) |
| 46 | + console.log('@@', matched) |
| 47 | + |
| 48 | + const first = matched[0] ?? undefined |
| 49 | + if (!isDashboard(first)) { |
| 50 | + matched = [ |
| 51 | + { |
| 52 | + path: '/dashboard', |
| 53 | + meta: { title: 'Dashboard' } |
| 54 | + } as any |
| 55 | + ].concat(matched) |
| 56 | + } |
| 57 | + |
| 58 | + breadList.list = matched.filter((item) => item.meta && item.meta.title) |
| 59 | + console.log('@@@', breadList) |
| 60 | +} |
| 61 | + |
| 62 | +getBreadcrumb() |
| 63 | + |
| 64 | +const handleLink = (item: any) => { |
| 65 | + const { redirect, path } = item |
| 66 | + console.log(path, redirect) |
| 67 | + |
| 68 | + if (redirect) { |
| 69 | + router.push(redirect).catch((err) => { |
| 70 | + console.warn(err) |
| 71 | + }) |
| 72 | + return |
| 73 | + } |
| 74 | + router.push(pathCompile(path)).catch((err) => { |
| 75 | + console.warn(err) |
| 76 | + }) |
| 77 | +} |
| 78 | +</script> |
| 79 | + |
| 80 | +<template> |
| 81 | + <el-breadcrumb class="app-breadcrumb" separator="/"> |
| 82 | + <transition-group name="breadcrumb"> |
| 83 | + <el-breadcrumb-item v-for="item in list" :key="item.path"> |
| 84 | + <a @click.prevent="handleLink(item)">{{ item.meta?.title }}</a> |
| 85 | + </el-breadcrumb-item> |
| 86 | + </transition-group> |
| 87 | + </el-breadcrumb> |
| 88 | +</template> |
| 89 | + |
| 90 | +<style scoped lang="less"> |
| 91 | +.app-breadcrumb.el-breadcrumb { |
| 92 | + display: inline-block; |
| 93 | + font-size: 14px; |
| 94 | + line-height: 50px; |
| 95 | + margin-left: 8px; |
| 96 | + |
| 97 | + .no-redirect { |
| 98 | + color: #97a8be; |
| 99 | + cursor: text; |
| 100 | + } |
| 101 | +} |
| 102 | +</style> |
0 commit comments