一、面包屑方案

1.核心:根据url生成面包屑数据动态渲染

2.生成数据规则:

(1)获取route.matched(匹配到的路由记录)

a.使用watch,组件创建时立马触发

b.监听route,改变时获取最新的面包屑数据

(2)根据meta.title和meta.icon来过滤要显示的菜单
  1. <script setup>
  2. import { watch, ref } from 'vue'
  3. import { useRoute } from 'vue-router'
  4. const route = useRoute()
  5. const breadcrumData = ref([])
  6. function generateData(routes) {
  7. return routes.filter((item) => item.meta.title && item.meta.icon)
  8. }
  9. // 监听route变化,生成新面包屑数据
  10. watch(
  11. route,
  12. () => {
  13. breadcrumData.value = generateData(route.matched)
  14. console.log(breadcrumData.value, 'breadcrumData.value')
  15. },
  16. {
  17. immediate: true
  18. }
  19. )
  20. </script>

二、方案步骤

1.创建面包屑组件

2.计算面包屑结构数据

一般面包屑最后一项不可点击

3.动态渲染面包屑

  1. <template>
  2. <el-breadcrumb class="breadcrumb" separator="/">
  3. <el-breadcrumb-item
  4. :to="{ path: item.path }"
  5. v-for="(item, index) in breadcrumData"
  6. :key="item.path"
  7. >
  8. <span
  9. :class="[
  10. index === breadcrumData.length - 1 ? 'no-rederict' : 'rederict'
  11. ]"
  12. >{{ item.meta.title }}</span
  13. >
  14. </el-breadcrumb-item>
  15. </el-breadcrumb>
  16. </template>
  17. <script setup>
  18. import { watch, ref } from 'vue'
  19. import { useRoute } from 'vue-router'
  20. import { useStore } from 'vuex'
  21. const route = useRoute()
  22. const breadcrumData = ref([])
  23. function generateData(routes) {
  24. return routes.filter((item) => item.meta.title && item.meta.icon)
  25. }
  26. // 监听route变化,生成新面包屑数据
  27. watch(
  28. route,
  29. () => {
  30. breadcrumData.value = generateData(route.matched)
  31. },
  32. {
  33. immediate: true
  34. }
  35. )
  36. // 获取主题颜色
  37. const store = useStore()
  38. const menuBgColor = ref(store.getters.cssVariables.menuBg)
  39. </script>
  40. <style lang="scss" scoped>
  41. .breadcrumb {
  42. margin-left: 8px;
  43. display: inline-block;
  44. line-height: 50px;
  45. font-size: 14px;
  46. .rederict {
  47. color: #666;
  48. cursor: pointer;
  49. }
  50. .rederict:hover {
  51. color: v-bind(menuBgColor);
  52. }
  53. ::v-deep .no-rederict {
  54. color: #97a8be;
  55. cursor: text;
  56. }
  57. }
  58. </style>

三、Transition和过度class

1.当插入或删除包含在 transition 组件中的元素时,vue会添加过度类名,检测触发js钩子函数

image.png

2.过度class用法

(1)-acitve类名可以为进入和离开动画设置不同的持续时间和动画函数

(2)enter-from 和 leave-to用来设置插入和删除时的样式
  1. /* 可以为进入和离开动画设置不同的持续时间和动画函数 */
  2. .slide-fade-enter-active {
  3. transition: all 0.3s ease-out;
  4. }
  5. .slide-fade-leave-active {
  6. transition: all 0.8s cubic-bezier(1, 0.5, 0.8, 1);
  7. }
  8. .slide-fade-enter-from,
  9. .slide-fade-leave-to {
  10. transform: translateX(20px);
  11. opacity: 0;
  12. }