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

1.需求:

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

2. 注意点

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

3. 代码操作


首先全部的代码 

<template>
    <nut-tabbar v-model="selected" class="tabbar-container" size="18px" @tab-switch="handleClick">
        <nut-tabbar-item :tab-title="item.title" :value="item.value" v-for="(item, index) in tabList" :key="index">
            <template #icon="props" style="position: relative">
                <img v-if="props.active" :src="item.activeImg" />
                <img v-else :src="item.img" />
            </template>
        </nut-tabbar-item>
    </nut-tabbar>
</template>
<script setup lang="ts">
import { storeToRefs } from "pinia";
import { reactive, onMounted, watch } from "vue";
import { useTabbarStore, useUserStore } from "../../store";
import Taro, { eventCenter } from "@tarojs/taro";
const tabbarStore = useTabbarStore();
const userStore = useUserStore();
const { selected, bottomDistance } = storeToRefs(tabbarStore);
const router = Taro.useRouter();

type menu = {
    name: string;
    title: string;
    pagePath: string;
    img: any;
    activeImg: any;
    value: number;
};
watch(selected, (newValue) => {
    // Taro.setStorageSync("selectedTab", newValue);
    tabbarStore.setSelected(newValue)
});
onMounted(() => {
    tabbarStore.setSelected(Taro.getStorageSync("selectedTab") || 0)
    eventCenter.once(router.onReady, () => {
        getTabbarHeight();
    });
});
const getTabbarHeight = () => {
    const query = Taro.createSelectorQuery()
        .select(".tabbar-container")
        .boundingClientRect();
    query.selectViewport();
    query.exec(function (res) {
        if (res[0]) {
            console.log(res);

            const height = res[0].height;
            tabbarStore.setTabbarHeight(height);
        }
    });
};
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,
    },

]);
const handleClick = (item, index: number) => {
    let url = tabList[index].pagePath;

    const pages = Taro.getCurrentPages()
    const currentPage = pages[pages.length - 1]
    if (currentPage.route !== url) {
        handleToPath(url);

    }


};
const handleToPath = (url) => {
    Taro.switchTab({
        url: url,
    });
};
</script>
<style lang="scss">
@import "./customTabBar.scss";
</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,

    },

]);、

跳转逻辑

const handleClick = (item, index: number) => {

    let url = tabList[index].pagePath;



    const pages = Taro.getCurrentPages()

    const currentPage = pages[pages.length - 1]

    if (currentPage.route !== url) {

        handleToPath(url);



    }




};

const handleToPath = (url) => {

    Taro.switchTab({

        url: url,

    });

};

// const pages = Taro.getCurrentPages()

    const currentPage = pages[pages.length - 1]

    if (currentPage.route !== url) {

        handleToPath(url);



    } 当前页面如果是当前的菜单 那么判断一下 根据页面的Api 查找当前的页面路径 当页面不存在 才跳转 这是优化问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unplugin-auto-import 是一个用于自动导入模块的 Vite 插件,可以帮助我们快速导入组件、函数、变量等等。在 Taro + Vue 3 + TypeScript 项目使用 unplugin-auto-import 自定义导入路径的步骤如下: 1. 安装所需依赖: ```bash npm install -D vite-plugin-auto-import ``` 2. 在 Vite 配置文件添加插件,并配置自定义导入: ```javascript import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import AutoImport from 'unplugin-auto-import/vite' export default defineConfig({ plugins: [ vue(), AutoImport({ // 配置需要自动导入的模块 imports: [ { // 导入 Taro 组件 path: '@tarojs/taro-vue', // 定义导入的名称 imports: ['AtButton', 'AtIcon'], }, { // 导入自定义组件 path: '@/components/MyCustomComponent.vue', // 定义导入的名称 imports: ['MyCustomComponent'], }, { // 导入函数 path: 'lodash/debounce', // 定义导入的名称 imports: ['debounce'], }, { // 导入变量 path: '@/config', // 定义导入的名称 imports: ['appConfig'], }, ], }), ], }) ``` 3. 在代码使用自定义导入的模块: ```vue <template> <view> <AtButton type="primary" @click="onClick">点击我</AtButton> <MyCustomComponent :text="text" /> </view> </template> <script lang="ts"> import { defineComponent, ref } from 'vue' import { AtButton, AtIcon } from '@tarojs/taro-vue' import { debounce } from 'lodash' import { appConfig } from '@/config' import MyCustomComponent from '@/components/MyCustomComponent.vue' export default defineComponent({ components: { AtButton, AtIcon, MyCustomComponent, }, setup() { const text = ref('Hello, Taro + Vue 3 + TypeScript!') const onClick = debounce(() => { console.log('click button') }, 500) return { text, onClick, } }, }) </script> ``` 注意:在使用 unplugin-auto-import 插件时,需要在 tsconfig.json 文件开启 "esModuleInterop": true,以支持模块导入。同时,在 Taro 使用 Vue 3 时,需要引入 "@tarojs/taro-vue" 组件库来替换原有的 "@tarojs/components" 组件库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值