Breadcrumb.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <div :class="[prefixCls, `${prefixCls}--${theme}`]">
  3. <a-breadcrumb :routes="routes">
  4. <template #itemRender="{ route, routes, paths }">
  5. <Icon :icon="route.meta.icon" v-if="getShowBreadCrumbIcon && route.meta.icon" />
  6. <span v-if="!hasRedirect(routes, route)">
  7. {{ t(route.meta.title) }}
  8. </span>
  9. <router-link v-else to="" @click="handleClick(route, paths, $event)">
  10. {{ t(route.meta.title) }}
  11. </router-link>
  12. </template>
  13. </a-breadcrumb>
  14. </div>
  15. </template>
  16. <script lang="ts">
  17. import type { RouteLocationMatched } from 'vue-router';
  18. import { defineComponent, ref, toRaw, watchEffect } from 'vue';
  19. import { Breadcrumb } from 'ant-design-vue';
  20. import { useI18n } from 'vue-i18n';
  21. import { useRouter } from 'vue-router';
  22. import { filter } from '/@/utils/helper/treeHelper';
  23. import { REDIRECT_NAME } from '/@/router/constant';
  24. import Icon from '/@/components/Icon';
  25. import { HomeOutlined } from '@ant-design/icons-vue';
  26. import { PageEnum } from '/@/enums/pageEnum';
  27. import { useDesign } from '/@/hooks/web/useDesign';
  28. import { useRootSetting } from '/@/hooks/setting/useRootSetting';
  29. import { propTypes } from '/@/utils/propTypes';
  30. import { useGo } from '/@/hooks/web/usePage';
  31. import { isString } from '/@/utils/is';
  32. export default defineComponent({
  33. name: 'LayoutBreadcrumb',
  34. components: { HomeOutlined, Icon, [Breadcrumb.name]: Breadcrumb },
  35. props: {
  36. theme: propTypes.oneOf(['dark', 'light']),
  37. },
  38. setup() {
  39. const routes = ref<RouteLocationMatched[]>([]);
  40. const { currentRoute } = useRouter();
  41. const { prefixCls } = useDesign('layout-breadcrumb');
  42. const { getShowBreadCrumbIcon } = useRootSetting();
  43. const { t } = useI18n();
  44. watchEffect(() => {
  45. if (currentRoute.value.name === REDIRECT_NAME) return;
  46. const matched = currentRoute.value?.matched;
  47. if (!matched || matched.length === 0) return;
  48. let breadcrumbList = filterItem(toRaw(matched));
  49. const filterBreadcrumbList = breadcrumbList.filter(
  50. (item) => item.path !== PageEnum.BASE_HOME
  51. );
  52. if (filterBreadcrumbList.length === breadcrumbList.length) {
  53. filterBreadcrumbList.unshift(({
  54. path: PageEnum.BASE_HOME,
  55. meta: {
  56. title: t('layout.header.home'),
  57. isLink: true,
  58. },
  59. } as unknown) as RouteLocationMatched);
  60. }
  61. if (currentRoute.value.meta?.currentActiveMenu) {
  62. filterBreadcrumbList.push((currentRoute.value as unknown) as RouteLocationMatched);
  63. }
  64. // routes.value = filterBreadcrumbList.length === 1 ? [] : filterBreadcrumbList;
  65. routes.value = filterBreadcrumbList;
  66. });
  67. function filterItem(list: RouteLocationMatched[]) {
  68. let resultList = filter(list, (item) => {
  69. const { meta } = item;
  70. if (!meta) {
  71. return false;
  72. }
  73. const { title, hideBreadcrumb, hideMenu } = meta;
  74. if (!title || hideBreadcrumb || hideMenu) {
  75. return false;
  76. }
  77. return true;
  78. }).filter((item) => !item.meta?.hideBreadcrumb || !item.meta?.hideMenu);
  79. resultList = resultList.filter((item) => item.path !== PageEnum.BASE_HOME);
  80. return resultList;
  81. }
  82. function handleClick(route: RouteLocationMatched, paths: string[], e: Event) {
  83. e?.preventDefault();
  84. const {
  85. children,
  86. redirect,
  87. meta,
  88. // components
  89. } = route;
  90. // const isParent =
  91. // components?.default?.name === 'DefaultLayout' || (components?.default as any)?.parentView;
  92. if (
  93. children?.length &&
  94. !redirect
  95. // && !isParent
  96. ) {
  97. e?.stopPropagation();
  98. return;
  99. }
  100. if (meta?.carryParam) {
  101. return;
  102. }
  103. const go = useGo();
  104. if (redirect && isString(redirect)) {
  105. go(redirect);
  106. } else {
  107. const ps = paths.slice(1);
  108. const lastPath = ps.pop() || '';
  109. const parentPath = ps.pop() || '';
  110. let path = `${parentPath}/${lastPath}`;
  111. path = /^\//.test(path) ? path : `/${path}`;
  112. go(path);
  113. }
  114. }
  115. function hasRedirect(routes: RouteLocationMatched[], route: RouteLocationMatched) {
  116. if (route?.meta?.isLink) {
  117. return true;
  118. }
  119. if (routes.indexOf(route) === routes.length - 1) {
  120. return false;
  121. }
  122. return true;
  123. }
  124. return { routes, t, prefixCls, getShowBreadCrumbIcon, handleClick, hasRedirect };
  125. },
  126. });
  127. </script>
  128. <style lang="less">
  129. @prefix-cls: ~'@{namespace}-layout-breadcrumb';
  130. .@{prefix-cls} {
  131. display: flex;
  132. padding: 0 8px;
  133. align-items: center;
  134. .ant-breadcrumb-link {
  135. .anticon {
  136. margin-right: 4px;
  137. margin-bottom: 2px;
  138. }
  139. }
  140. &--light {
  141. .ant-breadcrumb-link {
  142. color: @breadcrumb-item-normal-color;
  143. a {
  144. color: rgba(0, 0, 0, 0.65);
  145. &:hover {
  146. color: @primary-color;
  147. }
  148. }
  149. }
  150. .ant-breadcrumb-separator {
  151. color: @breadcrumb-item-normal-color;
  152. }
  153. }
  154. &--dark {
  155. .ant-breadcrumb-link {
  156. color: rgba(255, 255, 255, 0.6);
  157. a {
  158. color: rgba(255, 255, 255, 0.8);
  159. &:hover {
  160. color: @white;
  161. }
  162. }
  163. }
  164. .ant-breadcrumb-separator,
  165. .anticon {
  166. color: rgba(255, 255, 255, 0.8);
  167. }
  168. }
  169. }
  170. </style>