微信小程序中便捷实现自定义底部tab栏

前言:很多东西不记录下,过段时间不用就容易忘...

一、实现步骤:
  1. 在根目录按规范创建底部tab的页面,
  2. 在app.json中配置,告诉项目使用的tab配置,
  3. 在tab指向的页面中,配置tab排列的顺序,通过onshow中配置加载tab的排列顺序,
二、完整代码:
  1. 根目录创建的自定义tab的代码

    bash 复制代码
    Component({
      data: {
        activeIndex: 0,
        tabList: [
    			{
            pagePath: 'pages/login/login',
            text: '首页',
            normalIcon: '../image/img_03.png',
            selectedIcon: '../image/img_04.png'
          },
          {
            pagePath: 'pages/wx-logo/wx-logo', 
            text: 'logo绘制',
            normalIcon: '../image/img_01.png',
            selectedIcon: '../image/img_02.png'
          },
         
        ]
      },
      methods: {
        switchTab(e) {
          const { index, path } = e.currentTarget.dataset;
          if (index === this.data.activeIndex) return;
    
          this.setData({ activeIndex: index });
          wx.switchTab({ url: `/${path}` }); 
        }
      }
    });
    bash 复制代码
    <view class="tab-bar">
      <view 
        wx:for="{{tabList}}" 
        wx:key="index" 
        class="tab-item {{activeIndex === index ? 'active' : ''}}"
        bindtap="switchTab"
        data-index="{{index}}"
        data-path="{{item.pagePath}}"
      >
        <!-- 图标区域:替换为你的图片路径 -->
        <image 
          src="{{activeIndex === index ? item.selectedIcon : item.normalIcon}}" 
          class="tab-icon"
        />
        <!-- 文字区域 -->
        <text class="tab-text">{{item.text}}</text>
      </view>
    </view>
    bash 复制代码
    /* TabBar 容器:固定底部,适配安全区域 */
    .tab-bar {
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      height: 124rpx;
      background: #ffffff;
      display: flex;
      border-top: 1px solid #e6e6e6;
      padding-bottom: env(safe-area-inset-bottom); /* 适配 iPhone 底部安全区域 */
      z-index: 999;
    }
    
    /* 单个 Tab 项:平分宽度,居中布局 */
    .tab-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      transition: all 0.2s ease; /* 平滑过渡动画 */
    }
    
    /* 图标样式:默认尺寸 24px,选中 28px(主流放大比例) */
    .tab-icon {
      width: 48rpx;
      height: 48rpx;
      margin-bottom: 2px;
      transition: all 0.2s ease;
    }
    .tab-item.active .tab-icon {
      width: 28px;
      height: 28px;
    }
    
    /* 文字样式:默认 12px #999,选中 14px 主题色(参考微信绿) */
    .tab-text {
      font-size: 20rpx;
      /* color: #999999; */
    	color: #b2aaa2;
      transition: all 0.2s ease;
    }
    .tab-item.active .tab-text {
      font-size: 24rpx;
      color: #ffca28; /* 选中颜色,可自行修改 */
      font-weight: 500; /* 选中时微加粗,增强视觉效果 */
    }

    json中官方说需要配置,我发现配不配都不影响,看个人

    bash 复制代码
    {
      "component": true
    }
  2. app.json中的配置

    bash 复制代码
    	"tabBar": {
        "custom": true, 
        "list": [
          { "pagePath": "pages/login/login", "text": "首页" },
          { "pagePath": "pages/wx-logo/wx-logo", "text": "logo绘制" }
        ]
      },
  3. 底部tab配置跳转的页面中的配置

    bash 复制代码
      onShow() {
    		 // 安全判断,防止 getTabBar 不存在时报错
    		 if (typeof this.getTabBar === 'function' && this.getTabBar()) {
          this.getTabBar().setData({
            activeIndex: 0 // 底部tab中的第1个显示
          })
        }
      },
三、总结:初次自定义tab的几个误区
  1. 直接给自定义的tab文件,随便取名和随便放在其他文件夹内,最后就是无效果,
  2. 在app.json中配置自定义tab的对应页面,以为底部tab的渲染顺序遵从这个数组内的排列顺序?
  3. 其实是遵循在具体页面路径中配置的activeIndex的值的大小,进行排列的,(有时候配置完,需要点击2下才能选中,就是这个下标配置有问题)
相关推荐
咸虾米_10 小时前
小程序流量主哪种广告最赚钱?各类广告收益与实操建议
微信小程序·小程序·小程序开发·流量主广告
show4332 天前
2026微信小程序视频转文字多语种识别引擎技术选型:22种方言25种外语挑战
微信小程序·小程序·音视频
MrFlySand_飞沙3 天前
uniapp开发微信小程序实现接入企业微信客服
微信小程序·小程序·uni-app·企业微信
码艺-Alimjan3 天前
JWT 的本质、误区与正确落地:一套不依赖任何框架的架构方法论-微信小程序代码案例-Api 安全性拉满
微信小程序·架构·notepad++
禁止摆烂_才浅3 天前
微信小程序高频面试题
前端·面试·微信小程序
show4333 天前
2026微信小程序批量处理视频文件架构方案:免费批量实测
微信小程序·小程序·架构
wordbaby3 天前
从双端内卷到一库多端:基于 pnpm + Taro 4 + RNOH 鸿蒙的 Monorepo 跨端架构实战
前端·微信小程序·app
码云之上5 天前
小程序 iOS 多输入框焦点乱序脱坑记
ios·微信小程序·taro
Greg_Zhong7 天前
微信小程序 + 腾讯云人体分析:从 0 到 1 实现 AI 抠图打卡合照(细节待更新~)
人工智能·微信小程序·腾讯云·ai抠图-ai打卡拍照
java1234_小锋9 天前
【免费】基于Python的微信小程序网约车(打车,在线叫车,移动出行)系统(FastAPI+Vue3) 锋哥原创出品,必属精品
微信小程序·小程序·fastapi·网约车系统·在线叫车系统