index.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <view class="hc-tabbar-box">
  3. <view class="hc-tabbar">
  4. <template v-for="item in tabbar" :key="item.path">
  5. <view class="item" :class="tabKey === item.path ? 'cur':''" @click="itemClick(item)">
  6. <view class="icon-box">
  7. <view class="icon">
  8. <text :class="item.icon"/>
  9. </view>
  10. </view>
  11. <view class="name">{{ item.name }}</view>
  12. </view>
  13. </template>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import {onMounted, ref} from "vue";
  19. import {onShow} from '@dcloudio/uni-app'
  20. const tabKey = ref('pages/index/index')
  21. const tabbar = [
  22. {path: 'pages/index/index', name: '应用主页', icon: 'i-material-symbols-gamepad-rounded'},
  23. {path: 'pages/task/index', name: '任务消息', icon: 'i-ooui-message'},
  24. {path: 'pages/my/index', name: '我的', icon: 'i-material-symbols-person'},
  25. ];
  26. const itemClick = ({path}) => {
  27. tabKey.value = path;
  28. uni.switchTab({
  29. url: '/' + path,
  30. })
  31. }
  32. //渲染完成
  33. onMounted(() => {
  34. setTabbarData();
  35. })
  36. const setTabbarData = () => {
  37. uni.hideTabBar({
  38. animation: false
  39. });
  40. let pages = getCurrentPages();
  41. let currentPage = pages[pages.length - 1]
  42. tabbar.forEach(({path}) => {
  43. if (path === currentPage.route) {
  44. tabKey.value = path;
  45. }
  46. })
  47. }
  48. onShow(() => {
  49. setTabbarData();
  50. })
  51. </script>
  52. <style scoped lang="scss">
  53. @import "./style.scss";
  54. </style>