当前位置:   article > 正文

Taro + vue3 + js + nutUI 框架中自定义tabbar的组件封装以及页面跳转的逻辑_nut-tabbar

nut-tabbar

1.需求:

  在H5 中需要封装一个自定义的tabbar 菜单跳转 通过nut-ui 进行二次封装

2. 注意点

  H5 中原生的tabbar 在ios 中会出现问题 所以进行 封装tabbar

3. 代码操作


首先全部的代码 

  1. <template>
  2. <nut-tabbar v-model="selected" class="tabbar-container" size="18px" @tab-switch="handleClick">
  3. <nut-tabbar-item :tab-title="item.title" :value="item.value" v-for="(item, index) in tabList" :key="index">
  4. <template #icon="props" style="position: relative">
  5. <img v-if="props.active" :src="item.activeImg" />
  6. <img v-else :src="item.img" />
  7. </template>
  8. </nut-tabbar-item>
  9. </nut-tabbar>
  10. </template>
  11. <script setup lang="ts">
  12. import { storeToRefs } from "pinia";
  13. import { reactive, onMounted, watch } from "vue";
  14. import { useTabbarStore, useUserStore } from "../../store";
  15. import Taro, { eventCenter } from "@tarojs/taro";
  16. const tabbarStore = useTabbarStore();
  17. const userStore = useUserStore();
  18. const { selected, bottomDistance } = storeToRefs(tabbarStore);
  19. const router = Taro.useRouter();
  20. type menu = {
  21. name: string;
  22. title: string;
  23. pagePath: string;
  24. img: any;
  25. activeImg: any;
  26. value: number;
  27. };
  28. watch(selected, (newValue) => {
  29. // Taro.setStorageSync("selectedTab", newValue);
  30. tabbarStore.setSelected(newValue)
  31. });
  32. onMounted(() => {
  33. tabbarStore.setSelected(Taro.getStorageSync("selectedTab") || 0)
  34. eventCenter.once(router.onReady, () => {
  35. getTabbarHeight();
  36. });
  37. });
  38. const getTabbarHeight = () => {
  39. const query = Taro.createSelectorQuery()
  40. .select(".tabbar-container")
  41. .boundingClientRect();
  42. query.selectViewport();
  43. query.exec(function (res) {
  44. if (res[0]) {
  45. console.log(res);
  46. const height = res[0].height;
  47. tabbarStore.setTabbarHeight(height);
  48. }
  49. });
  50. };
  51. const tabList = reactive<menu[]>([
  52. {
  53. name: "home",
  54. title: "购票",
  55. pagePath: "/pages/index/index",
  56. img: require("../../assets/tabbar/2/3.png"),
  57. activeImg: require("../../assets/tabbar/1/3.png"),
  58. value: 0,
  59. },
  60. // {
  61. // name: "action",
  62. // title: "订单",
  63. // pagePath: "/pages/order/index",
  64. // img: require("../../assets/tabbar/2/5.png"),
  65. // activeImg: require("../../assets/tabbar/1/5.png"),
  66. // value: 0,
  67. // },
  68. {
  69. name: "report",
  70. title: "我的",
  71. pagePath: "/pages/my/index",
  72. img: require("../../assets/tabbar/2/4.png"),
  73. activeImg: require("../../assets/tabbar/1/4.png"),
  74. value: 0,
  75. },
  76. ]);
  77. const handleClick = (item, index: number) => {
  78. let url = tabList[index].pagePath;
  79. const pages = Taro.getCurrentPages()
  80. const currentPage = pages[pages.length - 1]
  81. if (currentPage.route !== url) {
  82. handleToPath(url);
  83. }
  84. };
  85. const handleToPath = (url) => {
  86. Taro.switchTab({
  87. url: url,
  88. });
  89. };
  90. </script>
  91. <style lang="scss">
  92. @import "./customTabBar.scss";
  93. </style>

4.解析

tabList: 菜单的内容数组  根据自己菜单的数量 来决定

const tabList = reactive<menu[]>([

    {

        name: "home",

        title: "购票",

        pagePath: "/pages/index/index",

        img: require("../../assets/tabbar/2/3.png"),

        activeImg: require("../../assets/tabbar/1/3.png"),

        value: 0,

    },

    // {

    //     name: "action",

    //     title: "订单",

    //     pagePath: "/pages/order/index",

    //     img: require("../../assets/tabbar/2/5.png"),

    //     activeImg: require("../../assets/tabbar/1/5.png"),

    //     value: 0,

    // },

    {

        name: "report",

        title: "我的",

        pagePath: "/pages/my/index",

        img: require("../../assets/tabbar/2/4.png"),

        activeImg: require("../../assets/tabbar/1/4.png"),

        value: 0,

    },

]);、

跳转逻辑

  1. const handleClick = (item, index: number) => {
  2.     let url = tabList[index].pagePath;
  3.     const pages = Taro.getCurrentPages()
  4.     const currentPage = pages[pages.length - 1]
  5.     if (currentPage.route !== url) {
  6.         handleToPath(url);
  7.     }
  8. };
  9. const handleToPath = (url) => {
  10.     Taro.switchTab({
  11.         url: url,
  12.     });
  13. };
  14. // const pages = Taro.getCurrentPages()
  15.     const currentPage = pages[pages.length - 1]
  16.     if (currentPage.route !== url) {
  17.         handleToPath(url);
  18.     } 当前页面如果是当前的菜单 那么判断一下 根据页面的Api 查找当前的页面路径 当页面不存在 才跳转 这是优化问题

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/751257
推荐阅读
相关标签
  

闽ICP备14008679号