navbar.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <a-layout-header class="header">
  3. <a-row style="width: 100%;" >
  4. <a-col :span="3" >
  5. <div class="logo" >
  6. <img :src="logoPng" alt="">
  7. </div>
  8. </a-col>
  9. <a-col :span="19" >
  10. <a-menu
  11. theme="dark"
  12. mode="horizontal"
  13. :style="{ lineHeight: '64px' }"
  14. v-model:selectedKeys="selectedKeys"
  15. >
  16. <a-menu-item
  17. v-for="route in appRouter.$state.router.navbar.route"
  18. :key="route.path"
  19. @click="changeRouter(route.path)"
  20. >
  21. {{route.name}}
  22. </a-menu-item>
  23. </a-menu>
  24. </a-col>
  25. <a-col :span="1" >
  26. <user />
  27. </a-col>
  28. </a-row>
  29. </a-layout-header>
  30. </template>
  31. <script lang="ts" setup >
  32. import { computed, onMounted, ref } from 'vue'
  33. import { useAppRouter } from '@/store/router'
  34. import user from './user.vue'
  35. import { useRouter, useRoute } from 'vue-router'
  36. import { routes } from '@/router/index'
  37. const logoPng = require('@/assets/logo.png')
  38. const router = useRouter()
  39. const route = useRoute()
  40. const appRouter = useAppRouter()
  41. const selectedKeys = ref<string[]>()
  42. const changeRouter = (path: string) => {
  43. selectedKeys.value = [path]
  44. appRouter.changeNavbar(path, '')
  45. }
  46. const hasCurrentRourte = (children: ROUTER.RoutesProps[]): boolean => {
  47. let r = false
  48. children.forEach(item => {
  49. if (item.path === route.path) {
  50. r = true
  51. }
  52. if (item.children && item.children.length) {
  53. item.children.forEach(_ => {
  54. if (_.path === route.path) {
  55. r = true
  56. }
  57. })
  58. }
  59. })
  60. return r
  61. }
  62. onMounted(() => {
  63. routes.forEach(item => {
  64. if (hasCurrentRourte(item.children)) {
  65. selectedKeys.value = [item.path]
  66. appRouter.changeNavbar(item.path, 'init')
  67. appRouter.changeSiderRoute()
  68. }
  69. })
  70. })
  71. </script>
  72. <style lang="less" scoped >
  73. .header {
  74. position: relative;
  75. z-index: 2;
  76. }
  77. .logo {
  78. width: 48px;
  79. height: 48px;
  80. img {
  81. width: 100%;
  82. height: 100%;
  83. }
  84. }
  85. </style>