MtaNavbar.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
  3. title="自定义导航栏" @clickLeft="handleBack">
  4. <!-- <image src="/static/logo.png" mode=""></image> -->
  5. <view>logo</view>
  6. <block slot="right" v-if="hasRight">
  7. <uni-icons type="bars" size="30" color="#fff" @click="handleChangeMenu"></uni-icons>
  8. </block>
  9. </uni-nav-bar>
  10. <!-- 菜单面板 -->
  11. <uni-collapse ref="collapse" accordion v-model="data.activeMenu" v-show="data.showMenu">
  12. <uni-collapse-item :title="item.title" v-for="item in data.list" :key="item.title" :name="item.url">
  13. <view v-for="cIt in item.children" :key="item.title" @click="handleClick(cIt)">
  14. <view class="content">
  15. <text class="text" :class="{active: cIt.url.includes(data.activePath)}">{{cIt.title}}</text>
  16. </view>
  17. </view>
  18. </uni-collapse-item>
  19. </uni-collapse>
  20. </template>
  21. <script setup>
  22. import {
  23. ref,
  24. reactive
  25. } from "vue"
  26. const showLeft = ref(null)
  27. import {
  28. onLoad
  29. } from "@dcloudio/uni-app"
  30. const props = defineProps({
  31. hasRight: {
  32. type: Boolean,
  33. default: true
  34. }
  35. })
  36. const ani = ref(null)
  37. const data = reactive({
  38. list: [{
  39. title: '1',
  40. url: '1',
  41. children: [{
  42. title: '1-1',
  43. url: '/pages/dqgzDangjiangongzuo/dqgzDangjiangongzuo'
  44. }, {
  45. title: '1-2',
  46. url: 'http://www.baidu.com',
  47. type: 'link'
  48. }, {
  49. title: '1-3',
  50. url: '/pages/index/index'
  51. }, {
  52. title: '1-4',
  53. url: '2'
  54. }, ]
  55. },
  56. {
  57. title: '2',
  58. url: '3',
  59. children: [{
  60. title: '2-1',
  61. url: '4'
  62. }, {
  63. title: '2-2',
  64. url: '5'
  65. }, {
  66. title: '2-3',
  67. url: '6'
  68. }, {
  69. title: '2-4',
  70. url: '7'
  71. }]
  72. }
  73. ],
  74. activeMenu: null,
  75. activePath: '',
  76. showMenu: false,
  77. })
  78. onLoad(() => {
  79. const pages = getCurrentPages();
  80. data.activePath = pages[pages.length - 1].route;
  81. const parentMenu = data.list.find(item => item.children.some(cit => cit.url.includes(data.activePath)));
  82. console.log('parentMenu && parentMenu.url', parentMenu && parentMenu.url)
  83. data.activeMenu = parentMenu && parentMenu.url;
  84. })
  85. function handleClick(data) {
  86. console.log(`菜单:${data.title};路径:${data.url}`);
  87. if (data.type == 'link') {
  88. window.location.href = data.url;
  89. return;
  90. }
  91. if (data.url) {
  92. uni.navigateTo({
  93. url: data.url
  94. })
  95. }
  96. }
  97. function handleChangeMenu() {
  98. data.showMenu = !data.showMenu;
  99. }
  100. function handleBack() {
  101. uni.navigateBack()
  102. }
  103. </script>
  104. <style>
  105. </style>