Breadcrumbs.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <div id = "breadcrumbs">
  3. <template v-for="(link, index) in links" :key="link.name">
  4. <router-link :to="link.href">{{ link.name }}</router-link>
  5. <span v-if="index < links.length - 1" class="separator">
  6. &raquo;
  7. </span>
  8. </template>
  9. </div>
  10. </template>
  11. <style scoped>
  12. span {
  13. color: #bbb;
  14. }
  15. a {
  16. text-decoration: none;
  17. padding: 0.4em;
  18. border-radius: 0.2em;
  19. }
  20. a:hover {
  21. text-decoration: underline;
  22. background-color: #000;
  23. }
  24. </style>
  25. <script setup>
  26. import { ref } from 'vue';
  27. import { watch } from 'vue';
  28. import { useRoute } from 'vue-router';
  29. const route = useRoute();
  30. const links = ref([]);
  31. watch(() => route.matched, (matched) => {
  32. links.value = [];
  33. matched.forEach((record) => {
  34. if (!record) return;
  35. if (record.meta && record.meta.breadcrumb) {
  36. record.meta.breadcrumb.forEach((item) => {
  37. links.value.push({
  38. name: item.name,
  39. href: item.href || record.path || '/'
  40. });
  41. });
  42. } else if (record.name) {
  43. links.value.push({
  44. name: record.name,
  45. href: record.path || '/'
  46. });
  47. }
  48. });
  49. }, { immediate: true });
  50. </script>