微信小程序custom-tab-bar

微信小程序自定义tab-bar。

tab-bar使用tdesign中的t-tab-bar

1 在项目下新建custom-tab-bar文件夹,新建index 组件。

1.1 index.wxml中增加t-tab-bar

wxml 复制代码
<t-tab-bar value="{{value}}"    bindchange="onChange" theme="tag" split="{{false}}">
  <t-tab-bar-item wx:for="{{list}}" wx:key="index"  value="{{item.value}}"   icon="{{item.icon}}">
    {{item.label}}
  </t-tab-bar-item>
</t-tab-bar>

1.2在index.js中增加以下代码

js 复制代码
 
Component({
    data: {
      value: 0,  
      list: [
        {value:0, pagePath: 'pages/home/index', label: '首页', icon: 'home' },
        {value:1, pagePath: 'pages/person-center/index', label: '个人中心', icon: 'user' }
      ],
    },
  
    methods: {
        init(){
            const page = getCurrentPages().pop();
            let a=this.data.list.find(item=>item.pagePath === page.route)
            this.setData({
                value:a.value
            });
          },
      onChange(e) {
            this.setData({
                value: e.detail.value
            });
            wx.switchTab({
                url: '/' + this.data.list[e.detail.value].pagePath
              });           
        }
    },
  });
  

2在app.json中配置tab

json 复制代码
"tabBar": {
        "selectedColor": "#33a3dc",
        "backgroundColor": "#ffffff",
        "color": "#000000",
        "custom": true,
        "list": [
            {
                "text": "首页",
                "pagePath": "pages/home/index"
            },
            {
                "text": "个人中心",
                "pagePath": "pages/person-center/index"
            }
        ]
    },
    "usingComponents": {
        "t-tab-bar": "tdesign-miniprogram/tab-bar/tab-bar",
        "t-tab-bar-item": "tdesign-miniprogram/tab-bar-item/tab-bar-item"
	}

3.在pages/home/index.js和pages/person-center/index.js中加入如下代码

js 复制代码
onShow() {
        this.getTabBar().init();
    },

编译后即可实现自定义tab-bar。

相关推荐
迷雾yx2 小时前
开发微信小程序 基础02
微信小程序·小程序
迷雾yx2 小时前
开发微信小程序 基础03
微信小程序·小程序
说私域3 小时前
地理定位营销与开源AI智能名片O2O商城小程序的融合与发展
人工智能·小程序
小雨cc5566ru13 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
小雨cc5566ru1 天前
hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
android·微信小程序·uni-app
技术闲聊DD1 天前
小程序原生-利用setData()对不同类型的数据进行增删改
小程序
康康爹1 天前
uniapp 小程序,登录上传头像昵称页面处理步骤
小程序·uni-app
小雨cc5566ru1 天前
微信小程序hbuilderx+uniapp+Android 新农村综合风貌旅游展示平台
android·微信小程序·uni-app
小雨cc5566ru1 天前
小程序 uniapp+Android+hbuilderx体育场地预约管理系统的设计与实现
android·小程序·uni-app
DK七七1 天前
【PHP陪玩系统源码】游戏陪玩系统app,陪玩小程序优势
前端·vue.js·游戏·小程序·php·uniapp