微信小程序入门【二】:自定义tabBar

 

一、在 app.json 中的 tabBar 项指定 custom 字段,同时其余 tabBar 相关配置也补充完整。

8cd792ea9ee54d0489cbf1b635e38af7.png

 

二、添加 tabBar 代码文件

在代码根目录(page同级目录)下添加入口文件:

custom-tab-bar/index.js

custom-tab-bar/index.json

custom-tab-bar/index.wxml

custom-tab-bar/index.wxss

注意:名称不可改

ee397bfe15424ff998054036a203acee.png

 

3. 编写 tabBar 代码

custom-tab-bar/index.wxml

<!--custom-tab-bar/index/index.wxml-->
<van-tabbar  active-color="#409EFF"  inactive-color="#000"  active="{{ active }}" bind:change="onChange">
  <van-tabbar-item wx:for="{{ list }}" wx:key="index" icon="{{ item.icon }}">{{
    item.text
  }}</van-tabbar-item>
</van-tabbar>

custom-tab-bar/index.js

// custom-tab-bar/index/index.js
Component({
    data: {
        active: 0,
		list: [
            {
                icon:"setting-o",
                text:"设置",
                url:"/pages/test/test"
            },
			{
				icon: 'friends-o',
				text: '测试',
				url: '/pages/coupon/coupon'
			},
			{
				icon: 'home-o',
				text: '关于',
				url: '/pages/index/index'
			},
		
		]
    },

	methods: {
		onChange(event) {
			this.setData({ active: event.detail });
			wx.switchTab({
				url: this.data.list[event.detail].url
			});
		},

		init() {
			const page = getCurrentPages().pop();
			this.setData({
				active: this.data.list.findIndex(item => item.url === `/${page.route}`)
			});
		}
	}
});

pages/index/index.js

 onShow(){
	this.getTabBar().init();
  },

注意:如需实现 tab 选中态,要在当前页面下,通过 getTabBar 接口获取组件实例,并调用 setData 更新选中态

效果图:

951a6d4e03bc4f06a29105224bbd0781.png

 

参考链接:https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.htmlhttps://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值