admin-vben5/packages/effects/layouts/src/widgets/breadcrumb.vue

73 lines
1.6 KiB
Vue
Raw Normal View History

2024-05-19 21:20:42 +08:00
<script lang="ts" setup>
import type { BreadcrumbStyleType } from '@vben/types';
2024-05-19 21:20:42 +08:00
import type { IBreadcrumb } from '@vben-core/shadcn-ui';
import { computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { $t } from '@vben/locales';
2024-08-31 14:11:01 +08:00
import { VbenBreadcrumbView } from '@vben-core/shadcn-ui';
2024-06-08 19:49:06 +08:00
2024-05-19 21:20:42 +08:00
interface Props {
hideWhenOnlyOne?: boolean;
showHome?: boolean;
showIcon?: boolean;
2024-06-01 23:15:29 +08:00
type?: BreadcrumbStyleType;
2024-05-19 21:20:42 +08:00
}
const props = withDefaults(defineProps<Props>(), {
showHome: false,
2024-05-19 21:20:42 +08:00
showIcon: false,
type: 'normal',
});
const route = useRoute();
const router = useRouter();
const breadcrumbs = computed((): IBreadcrumb[] => {
const matched = route.matched;
const resultBreadcrumb: IBreadcrumb[] = [];
for (const match of matched) {
const { meta, path } = match;
2024-05-19 21:20:42 +08:00
const { hideChildrenInMenu, hideInBreadcrumb, icon, name, title } =
meta || {};
if (hideInBreadcrumb || hideChildrenInMenu || !path) {
continue;
}
resultBreadcrumb.push({
icon,
2024-05-19 21:20:42 +08:00
path: path || route.path,
title: title ? $t((title || name) as string) : '',
2024-05-19 21:20:42 +08:00
});
}
if (props.showHome) {
resultBreadcrumb.unshift({
icon: 'mdi:home-outline',
isHome: true,
path: '/',
});
}
if (props.hideWhenOnlyOne && resultBreadcrumb.length === 1) {
return [];
}
2024-06-23 23:18:55 +08:00
2024-05-19 21:20:42 +08:00
return resultBreadcrumb;
});
function handleSelect(path: string) {
router.push(path);
}
</script>
<template>
2024-08-31 14:11:01 +08:00
<VbenBreadcrumbView
2024-05-19 21:20:42 +08:00
:breadcrumbs="breadcrumbs"
:show-icon="showIcon"
2024-08-31 14:11:01 +08:00
:style-type="type"
2024-06-09 13:31:43 +08:00
class="ml-2"
2024-05-19 21:20:42 +08:00
@select="handleSelect"
/>
</template>