breadcrumb.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. <script lang="ts">
  2. import { IObject } from "@/types/interface";
  3. import { getValueByKeys } from "@/utils/utils";
  4. import { defineComponent, ref, watch } from "vue";
  5. import { RouteLocationMatched, useRouter } from "vue-router";
  6. /**
  7. * 顶部面包屑
  8. */
  9. export default defineComponent({
  10. name: "Breadcrumb",
  11. setup() {
  12. const router = useRouter();
  13. const breadcrumbs = ref<IObject[]>([]);
  14. const { currentRoute } = router;
  15. const firstRoute = (router.options.routes[0] || {}) as RouteLocationMatched;
  16. const home: RouteLocationMatched = firstRoute.children && firstRoute.children.length > 0 ? (firstRoute.children[0] as RouteLocationMatched) : firstRoute;
  17. watch(
  18. () => currentRoute.value,
  19. () => {
  20. breadcrumbs.value = currentRoute.value.path !== home.path ? getValueByKeys(currentRoute.value, "meta.matched", []) : [];
  21. }
  22. );
  23. return { breadcrumbs, currentRoute, home };
  24. }
  25. });
  26. </script>
  27. <template>
  28. <el-breadcrumb separator="/" style="padding-top: 4px">
  29. <el-breadcrumb-item :to="{ path: home.path }"> 主页 </el-breadcrumb-item>
  30. <el-breadcrumb-item v-for="x in breadcrumbs" :key="x.path">{{ currentRoute.query._mt || x.title || "" }} </el-breadcrumb-item>
  31. </el-breadcrumb>
  32. </template>