|
| 1 | +<template lang="pug"> |
| 2 | +div |
| 3 | + a-breadcrumb(seprator="/") |
| 4 | + a-breadcrumb-item(v-for="(route,index) in breadList",:key="route.breadcrumbName") |
| 5 | + router-link(:to="route.path",@click.native="handleClick(route,index)") {{route.breadcrumbName}} |
| 6 | +</template> |
| 7 | + |
| 8 | +<script lang="ts"> |
| 9 | +/** |
| 10 | + * 方案 |
| 11 | + * 1、通过路由文件的子父级关系确定面包屑的显示。即通过路由地址在路由文件中获得到子父关系 |
| 12 | + * 2、面包屑的层级关系,直接手动操作。 |
| 13 | + * ——面包屑显示的时候,只需要根据list直接显示就可以,不需要考虑路由的层级关系 |
| 14 | + * # 每次加载从storage里取,然后再渲染——init——为了处理刷新情况 |
| 15 | + * # 在组件调用时,向list中添加 |
| 16 | + * # 点击时,删除后面的 |
| 17 | + */ |
| 18 | +import { |
| 19 | + computed, defineComponent, onMounted, |
| 20 | +} from 'vue'; |
| 21 | +import { useStore } from 'vuex'; |
| 22 | +import { Breadcrumb } from 'ant-design-vue'; |
| 23 | +import { useRoute } from 'vue-router'; |
| 24 | +// import getDefaultRoutes from '@/router/defaultRoutes'; |
| 25 | +import { Bread } from '@/types/base'; |
| 26 | +// import { UserRole } from '@/types/user'; |
| 27 | +
|
| 28 | +export default defineComponent({ |
| 29 | + components: { |
| 30 | + aBreadcrumb: Breadcrumb, |
| 31 | + aBreadcrumbItem: Breadcrumb.Item, |
| 32 | + }, |
| 33 | +
|
| 34 | + setup() { |
| 35 | + const route = useRoute(); |
| 36 | + const store = useStore(); |
| 37 | + const breadList = computed(() => store.state.breadcrumb.breadList); |
| 38 | + // const role = sessionStorage.getItem('role') as UserRole | null; |
| 39 | +
|
| 40 | + function handleClick(r: Bread, bIndex: number) { |
| 41 | + if (bIndex < breadList.value.length - 1) { |
| 42 | + // splice(bIndex + 1); |
| 43 | + store.dispatch('breadcrumb/splice', bIndex); |
| 44 | + } |
| 45 | + } |
| 46 | +
|
| 47 | + const selfMounted = async () => { |
| 48 | + // await init(); |
| 49 | + store.dispatch('breadcrumb/init'); |
| 50 | + // console.log('init', toRaw(breadList.value)); |
| 51 | + // const list = getDefaultRoutes(role); |
| 52 | + // if (list.find((r) => r.path === route.path)) { |
| 53 | + // // clear(); |
| 54 | + // store.dispatch('breadcrumb/clear'); |
| 55 | + // } |
| 56 | + // console.log('clear', toRaw(breadList.value)); |
| 57 | +
|
| 58 | + // 未点击面包屑时,自动删除不需要的 面包屑 |
| 59 | + const { path } = route; |
| 60 | + const index = breadList.value.findIndex((p) => p.path.includes(path)); |
| 61 | + if (index > -1) { |
| 62 | + // splice(index + 1); |
| 63 | + store.dispatch('breadcrumb/splice', index); |
| 64 | + } |
| 65 | +
|
| 66 | + /** 开始处理面包值 */ |
| 67 | + store.dispatch('breadcrumb/push', { |
| 68 | + breadcrumbName: '实验管理', |
| 69 | + path: route.path, |
| 70 | + }); |
| 71 | + // console.log('splice', toRaw(breadList.value)); |
| 72 | + }; |
| 73 | + onMounted(() => { |
| 74 | + selfMounted(); |
| 75 | + }); |
| 76 | +
|
| 77 | + return { |
| 78 | + handleClick, |
| 79 | + breadList, |
| 80 | + }; |
| 81 | + }, |
| 82 | +}); |
| 83 | +</script> |
| 84 | + |
| 85 | +<style lang="stylus" scoped> |
| 86 | +.bcr-wrap |
| 87 | + font-size 14px |
| 88 | +</style> |
0 commit comments