uin-app微信小程序自定义tabBar底部菜单实现简单示例(工作笔记)

在微信小程序中实现自定义 tabBar 可以为你的应用提供更加灵活和个性化的底部导航菜单。由于微信小程序的官方 tabBar 配置功能有限,自定义 tabBar 成为了很多开发者实现复杂底部导航的选择。以下是一个简单的示例,说明如何在小程序中实现自定义 tabBar。

步骤 1: 配置 app.json

首先,你需要在 app.json 中配置 tabBar 的基础信息,但 custom 字段需要设置为 true 来启用自定义 tabBar。

json 复制代码
{
  "pages": [
    "pages/index/index",
    "pages/logs/logs",
    "pages/profile/profile"
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "tabBar": {
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "日志"
      },
      {
        "pagePath": "pages/profile/profile",
        "text": "我的"
      }
    ],
    "custom": true,
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black"
  }
}

步骤 2: 创建自定义 tabBar 组件

在项目的根目录下创建一个名为 custom-tab-bar 的文件夹,并在其中创建所需的文件(如 index.js、index.json、index.wxml、index.wxss)。

index.js

这里可以处理逻辑,比如根据当前页面切换 tab。

javascript 复制代码
Component({
  data: {
    selected: 0,
    list: [{
      pagePath: '/pages/index/index',
      text: '首页'
    }, {
      pagePath: '/pages/logs/logs',
      text: '日志'
    }, {
      pagePath: '/pages/profile/profile',
      text: '我的'
    }]
  },
  methods: {
    switchTab: function(e) {
      const data = e.currentTarget.dataset;
      if (this.data.selected === data.index) return;
      wx.switchTab({url: data.path});
    },
    onShow: function(e) {
      // 当 tabBar 显示时触发,可以根据需要获取当前页面路径
      const { path } = getCurrentPages().pop();
      this.setData({
        selected: this.data.list.findIndex(tab => tab.pagePath === path)
      });
    }
  }
});
index.json

定义组件的配置信息。

json 复制代码
{
  "usingComponents": {}
}
index.wxml

定义 tabBar 的结构。

html 复制代码
<view class="tab-bar">
  <block wx:for="{{list}}" wx:key="index" wx:for-index="idx">
    <view class="tab-item {{selected === idx ? 'active' : ''}}" data-path="{{item.pagePath}}" data-index="{{idx}}" bindtap="switchTab">
      {{item.text}}
    </view>
  </block>
</view>
index.wxss

添加样式。

css 复制代码
.tab-bar {
  display: flex;
  justify-content: space-around;
  background-color: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
}

.tab-item {
  padding: 10px 0;
  text-align: center;
}

.tab-item.active {
  color: #3cc51f;
}

步骤 3: 在 app.js 中使用自定义 tabBar

在 app.js 中引入并使用自定义的 tabBar 组件。

javascript 复制代码
App({
  onLaunch: function () {
    // 在这里可以执行一些启动时的逻辑
  },
  usingComponents: {
    '不同角色,控制查看底部菜单权限
    实现效果1:实体店![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2ca6098eefaf4d55935d9eb8d44116fb.png)
实现效果2:回收公司
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/da1d7b225c064d3ab058176211f65833.png)
相关推荐
远离UE46 小时前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5
keyipatience7 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
hanlin037 小时前
刷题笔记:力扣第32题-最长有效括号
笔记·算法·leetcode
xian_wwq8 小时前
【学习笔记】Milvus 如何完成一次向量相似度检索,Milvus 索引到底应该怎么选?-15
笔记·学习·milvus
SensorFlow11 小时前
曝光埋点别放在组件渲染里:连续可见与去重范围怎么验收
javascript
harris148154352975811 小时前
能解码不等于能信任:JWT 的签名到底验的是什么
javascript
liqingdi43711 小时前
[SWPUCTF 2023 秋季新生赛]Easy SMC
服务器·前端·javascript
日耗10亿Token被老板赶出门11 小时前
REA 火了:一个 Electron 按钮的调用链,它能追到哪一步?
javascript·electron
wujiuhsu11 小时前
图片里的简体中文怎么翻译成繁体?商品图、详情页和海报批量转换方法
经验分享·笔记
青柠之夏cc12 小时前
Laravel服务容器底层原理揭秘
javascript·rpc·laravel