HcTopMenu.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <el-scrollbar>
  3. <div class="hc-header-top-menu-bar">
  4. <template v-for="(item, index) in topMenuData" :key="index">
  5. <div class="item" :class="curKey === item?.code ? 'cur' : '' " @click="topMenuClick(item)">
  6. <span>{{ item?.name }}</span>
  7. <el-badge v-if="item?.code === 'tasks' && taskCounts > 0" :value="taskCounts" />
  8. </div>
  9. </template>
  10. </div>
  11. </el-scrollbar>
  12. </template>
  13. <script setup>
  14. import { ref, watch } from 'vue'
  15. import { useRoute } from 'vue-router'
  16. import { useAppStore } from '~src/store'
  17. import HcTopMenu from '~src/plugins/HcTopMenu'
  18. import { getArrValue } from 'js-fast-way'
  19. const props = defineProps({
  20. msgCount: {
  21. type: [Number, String],
  22. default: 0,
  23. },
  24. })
  25. const emit = defineEmits(['change', 'load'])
  26. //初始组合式
  27. const useRoutes = useRoute()
  28. const store = useAppStore()
  29. //处理菜单数据
  30. const setMenuItem = async (item) => {
  31. emit('change', await HcTopMenu.setMenuItem(item))
  32. }
  33. //监听菜单数据
  34. const taskCounts = ref(props.msgCount)
  35. watch(() => props.msgCount, (val) => {
  36. taskCounts.value = val ?? 0
  37. }, { immediate: true, deep: true })
  38. //监听菜单数据
  39. const topMenuData = ref([])
  40. watch(() => store.getMenus, (val) => {
  41. topMenuData.value = getArrValue(val)
  42. }, { immediate: true, deep: true })
  43. //监听路由数据
  44. const curKey = ref('')
  45. watch(() => useRoutes, (val) => {
  46. HcTopMenu.initMenu({
  47. routes: val,
  48. menu: topMenuData.value,
  49. load: (key) => {
  50. curKey.value = key
  51. emit('load', key)
  52. },
  53. change: (key, item) => {
  54. curKey.value = key
  55. setMenuItem(item)
  56. },
  57. })
  58. }, { immediate: true, deep: true })
  59. //菜单被点击
  60. const topMenuClick = (item) => {
  61. setMenuItem(item)
  62. }
  63. </script>
  64. <style lang="scss">
  65. .hc-header-top-menu-bar {
  66. position: relative;
  67. height: 100%;
  68. display: flex;
  69. align-items: center;
  70. .item {
  71. position: relative;
  72. cursor: pointer;
  73. padding: 6px 8px;
  74. border-radius: 3px;
  75. color: #efefef;
  76. transition: background .2s, color .2s;
  77. &:hover {
  78. color: white;
  79. background: var(--el-color-primary-dark-2);
  80. }
  81. &.cur {
  82. color: white;
  83. background: var(--el-color-primary-dark-2);
  84. }
  85. }
  86. .item + .item {
  87. margin-left: 2px;
  88. }
  89. .el-badge__content {
  90. top: -13px;
  91. right: -4px;
  92. }
  93. }
  94. </style>