navbar.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <a-layout-header class="header" :style="{backgroundColor: headerBgColor}">
  3. <a-row >
  4. <a-col :span="18" >
  5. <a-menu
  6. mode="horizontal"
  7. :style="{ lineHeight: '64px', border: 'none' }"
  8. :selectedKeys="selectedKeys"
  9. >
  10. <a-menu-item
  11. v-for="route in appRouter.$state.router.navbar.route"
  12. :key="route.path"
  13. @click="changeRouter(route)"
  14. >
  15. {{route.name}}
  16. </a-menu-item>
  17. </a-menu>
  18. </a-col>
  19. <a-col :span="4" >
  20. <a-row :gutter="[8, 8]" justify="end" >
  21. <a-col class="df-center" >
  22. <a-button type="text" @click="changeTheme" > <IconTsx :name="iconName" /></a-button>
  23. </a-col>
  24. <a-col class="df-center" >
  25. <user />
  26. </a-col>
  27. </a-row>
  28. </a-col>
  29. </a-row>
  30. </a-layout-header>
  31. </template>
  32. <script lang="ts" setup >
  33. import { computed, onMounted, ref } from 'vue'
  34. import { useAppRouter } from '@/store/router'
  35. import user from './user.vue'
  36. import { useRouter, useRoute } from 'vue-router'
  37. import { routes } from '@/router/index'
  38. import { IconTsx } from '@/components/MicroComponents/index'
  39. import { useDesignStore } from '@/store'
  40. const logoPng = require('@/assets/logo.png')
  41. const router = useRouter()
  42. const route = useRoute()
  43. const appRouter = useAppRouter()
  44. const selectedKeys = ref<string[]>()
  45. const designStore = useDesignStore()
  46. console.log('designStore:', !!designStore.theme, designStore.theme)
  47. const iconName = computed(() => designStore.theme ? 'sun' : 'moon')
  48. const headerBgColor = computed(() => designStore.theme ? '#141414' : '#fff')
  49. const changeTheme = () => {
  50. designStore.changeModeltheme()
  51. }
  52. const changeRouter = (route: ROUTER.RoutesProps) => {
  53. if (route.link) {
  54. window.open(route.path)
  55. } else {
  56. selectedKeys.value = [route.path]
  57. appRouter.changeNavbar(route.path, '')
  58. }
  59. }
  60. const hasCurrentRourte = (children: ROUTER.RoutesProps[]): boolean => {
  61. let r = false
  62. children.forEach(item => {
  63. if (item.path === route.path) {
  64. r = true
  65. }
  66. if (item.children && item.children.length) {
  67. item.children.forEach(_ => {
  68. if (_.path === route.path) {
  69. r = true
  70. }
  71. })
  72. }
  73. })
  74. return r
  75. }
  76. onMounted(() => {
  77. routes.forEach(item => {
  78. if (item.children && hasCurrentRourte(item.children)) {
  79. selectedKeys.value = [item.path]
  80. appRouter.changeNavbar(item.path, 'init')
  81. appRouter.changeSiderRoute()
  82. }
  83. })
  84. })
  85. </script>
  86. <style lang="less" scoped >
  87. .header {
  88. // width: 100%;
  89. // position: absolute;
  90. // z-index: 2;
  91. // background-color: #fff;
  92. }
  93. .df-center {
  94. display: flex;
  95. justify-content: center;
  96. align-items: center;
  97. }
  98. </style>