小程序自定义 tabbar 以vant weapp为例

1.首先建立 custom-tab-bar 文件夹  包含 index.js  index.json index.wxml

// index.js文件
Component({ data: { active:
0, list: [ { "url": "/pages/index/index", "icon": "wap-home-o", "text": "首页" }, { "url": "/pages/index/prize", "icon": "orders-o", "text": "奖品" }, { "url": "/pages/index/exchange", "icon": "points", "text": "兑换" }, { "url": "/pages/index/my", "icon": "user-circle-o", "text": "我的" } ] }, methods: { onChange(e) { console.log(e,'e') this.setData({ active: e.detail }); wx.switchTab({ url: this.data.list[e.detail].url }); }, init() { const page = getCurrentPages().pop(); this.setData({   active: this.data.list.findIndex(item => item.url === `/${page.route}`) }); } } });

index.json文件

{ "component": true, "usingComponents": { "van-tabbar": "@vant/weapp/tabbar/index", "van-tabbar-item": "@vant/weapp/tabbar-item/index", "van-icon": "@vant/weapp/icon/index" } }

index.wxml文件

<van-tabbar active="{{ active }}" bind:change="onChange"> <van-tabbar-item wx:for="{{ list }}" wx:key="index" icon="{{ item.icon }}">{{ item.text }}</van-tabbar-item> </van-tabbar>
/** * 生命周期函数--监听页面显示 在每个tabbar页面onshow 调用即可; */ onShow: function () { console.log('个人中心') this.getTabBar().init(); },

 app.json文件

{ "pages": [ "pages/index/index", "pages/index/prize", "pages/index/exchange", "pages/index/my" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" }, "usingComponents": { }, "tabBar": { "custom": true, "color": "#000000", "selectedColor": "#000000", "backgroundColor": "#000000", "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/index/prize", "text": "奖品" }, { "pagePath": "pages/index/exchange", "text": "兑换" }, { "pagePath": "pages/index/my", "text": "我的" } ] }, "sitemapLocation": "sitemap.json" }

 原创未经允许不得转载!!!转载请注明出处~谢谢合作;


__EOF__

本文作者蓝色帅-橙子哥
本文链接https://www.cnblogs.com/lhl66/p/12870139.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   鱼樱前端  阅读(8424)  评论(6编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
 
拂雪 - 不才
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.
  1. 1 往后余生 王贰浪
  2. 2 拂雪 不才
  3. 3 我的一个道姑朋友 洛尘鞅
  4. 4 大田后生仔 王雨萌
点击右上角即可分享
微信分享提示