• taro3 hooks 使用vant ui 自定义微信小程序tabber


    一个大坑 填完了已经

    问题描述: 自定义的tabber 切换 点击两次才会切换成功~~~

    解决方案 存放在dva中处理

    前期准备

    dva 安装 已经安装跳过

    https://www.jianshu.com/p/c2050b8ebb6f

    Ⅰ- 壹 - 描述

    必须开启
    custom: true,//启动自定义tabBar

    list 下文件index.config.ts下必须添加 “usingComponents”: {}

    https://taro-docs.jd.com/taro/docs/custom-tabbar#1-%E5%8E%9F%E7%94%9F%E5%86%99%E6%B3%95

    export default definePageConfig({
      navigationBarTitleText: '首页',
      "usingComponents": {}
    })
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    Ⅱ - 贰 - 封装代码

    文件目录

    创建文件夹custom-tab-bar
    放在与pages同级目录下(必须这样 微信规定的)
    在这里插入图片描述

    index.config.ts

    export default {
      "component": true
    }
    
    • 1
    • 2
    • 3

    index.tsx

    import { useEffect } from 'react'
    import Taro from '@tarojs/taro'
    import { Tabbar, TabbarItem } from '@antmjs/vantui'
    import './index.less'
    import { useDispatch, useSelector } from 'react-redux';
    const Index = () => {
      const dispatch = useDispatch();
      const tabberArr :any= useSelector<any>(
        (state) => state.customtabbar.tabberArr
      );
      return (
        <>
          <Tabbar active={tabberArr.selected} >
            {tabberArr.list.map((item, index) => {
              return (
                <TabbarItem key={index} icon={item.iconPath} onClick={() => {
                  dispatch({
                    type:'customtabbar/emitSelected',
                    payload:index
                  })
                  Taro.switchTab({ url: item.pagePath })
                }}>{item.text}{tabberArr.selected}</TabbarItem>
              )
            })}
          </Tabbar>
        </>
      )
    }
    export default Index
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    model.ts

    export default {
      namespace: 'customtabbar',
      state: {
        tabberArr: {
          selected: 0,
          color: '#000000',
          selectedColor: '#DC143C',
          list: [
            {
              id: 0,
              pagePath: '/pages/index/index',
              // selectedIconPath: '../images/tabbar_home_on.png',
              iconPath: 'home-o',
              text: '首页'
            },
            {
              id: 1,
              pagePath: '/pages/my/index',
              // selectedIconPath: '../images/tabbar_my_on.png',
              iconPath: 'friends-o',
              text: '个人中心'
            }
          ]
        }
      },
      effects: {},
      //方法 dispatch 触发
      reducers: {
        emitSelected(state, { payload }) {
          return { ...state, tabberArr:{ ...state.tabberArr, selected: payload } };
        },
      },
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    C++(QT)画图行车
    【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
    《Python编程:从入门到实战》学习笔记 第4章 操作列表
    【cmake】find_package设置查找路径
    ubuntu20.04配置vscode
    华为云 云耀云服务器L实例评测|使用宝塔一键部署自己专属网站
    BOMBLAB
    tsp可视化python
    Redis 常用基本命令
    Flink 反压
  • 原文地址:https://blog.csdn.net/weixin_42863800/article/details/126035307