千家信息网

微信小程序怎么根据不同用户切换不同TabBar

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,今天小编给大家分享一下微信小程序怎么根据不同用户切换不同TabBar的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收
千家信息网最后更新 2026年11月03日微信小程序怎么根据不同用户切换不同TabBar

今天小编给大家分享一下微信小程序怎么根据不同用户切换不同TabBar的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

现有需求:

小程序用户有三种身份(公众、运维人员、领导),根据不同用户身份显示不同的tabbar

众所周知微信小程序全局文件app.json里面的"tabBar"里面的list只能放置2-5个,要想实现3个tabbar,必须得复用tabbar,三种身份都需要个人中心,剩下的是长列表(两个),表单,图表 刚好是5个,废话少说,上代码

1全局.app.json

tabbar里面的sustom要设置为true

"custom": true,
{  "pages": [    xxxxxx:xxxxxx     ],  "window": {   xxxxxxxxx  },  "tabBar": {    "custom": true,    "color": "#7A7E83",    "selectedColor": "#d81e06",    "borderStyle": "black",    "backgroundColor": "#ffffff",    "list": [      {        "pagePath": "pages/Users/myrepaire/myrepaire",        "text": "我要报修",        "iconPath": "/images/tabbar/weixiu1.png",        "selectedIconPath": "/images/tabbar/weixiu2.png"      },      {        "pagePath": "pages/Users/myMalfunction/myMalfunction",        "text": "我的故障",        "iconPath": "/images/tabbar/myweixiu1.png",        "selectedIconPath": "/images/tabbar/myweixiu2.png"      },      {        "pagePath": "pages/logs/logs",        "text": "个人中心",        "iconPath": "/images/tabbar/user1.png",        "selectedIconPath": "/images/tabbar/user2.png"      },      {        "pagePath": "pages/weixiu/myweixiu/myweixiu",        "text": "我的维修",        "iconPath": "/images/tabbar/myweixiu1.png",        "selectedIconPath": "/images/tabbar/myweixiu1.png"      },      {        "pagePath": "pages/charts/charts",        "text": "故障报表",        "iconPath": "/images/tabbar/chart1.png",        "selectedIconPath": "/images/tabbar/chart2.png"      }    ]  },  "sitemapLocation": "sitemap.json"}

可以看到全局app.json里面放了5个不同的tabbar路径

2.自定义custom-tab-bar

(详见微信官方文档)

index.js

Component({  data: {    selected: 0,    color: "#000000",    roleId: '',    selectedColor: "#1396DB",    allList: [{      list1: [{        pagePath: "/pages/Users/myrepaire/myrepaire",        iconPath: "/images/tabbar/weixiu1.png",        selectedIconPath: "/images/tabbar/weixiu2.png",        text: "我要报修"      }, {        pagePath: "/pages/Users/myMalfunction/myMalfunction",        iconPath: "/images/tabbar/myweixiu1.png",        selectedIconPath: "/images/tabbar/myweixiu2.png",        text: "我的故障"      }, {        pagePath: "/pages/logs/logs",        text: "个人中心",        iconPath: "/images/tabbar/user1.png",        selectedIconPath: "/images/tabbar/user2.png"      }],      list2: [{        pagePath: "/pages/weixiu/myweixiu/myweixiu",        iconPath: "/images/tabbar/weixiu1.png",        selectedIconPath: "/images/tabbar/weixiu2.png",        text: "我要维修"      }, {        pagePath: "/pages/Users/myMalfunction/myMalfunction",        iconPath: "/images/tabbar/myweixiu1.png",        selectedIconPath: "/images/tabbar/myweixiu2.png",        text: "我的维修"      }, {        pagePath: "/pages/logs/logs",        text: "个人中心",        iconPath: "/images/tabbar/user1.png",        selectedIconPath: "/images/tabbar/user2.png"      }],      list3: [{        pagePath: "/pages/Users/myrepaire/myrepaire",        iconPath: "/images/tabbar/weixiu1.png",        selectedIconPath: "/images/tabbar/weixiu2.png",        text: "我要报修"      }, {        pagePath: "/pages/charts/charts",        iconPath: "/images/tabbar/chart1.png",        selectedIconPath: "/images/tabbar/chart2.png",        text: "故障报表"      }, {        pagePath: "/pages/logs/logs",        text: "个人中心",        iconPath: "/images/tabbar/user1.png",        selectedIconPath: "/images/tabbar/user2.png"      }]    }],    list: []  },  attached() {    const roleId = wx.getStorageSync('statu')    if (roleId == 20) {      this.setData({        list: this.data.allList[0].list1      })    }else if(roleId==5){      this.setData({        list: this.data.allList[0].list3      })    }else if(roleId==102){      this.setData({        list: this.data.allList[0].list2      })    }  },  methods: {    switchTab(e) {      const data = e.currentTarget.dataset      const url = data.path      wx.switchTab({ url })      this.setData({        selected: data.index      })    }  },})

分析:

  1. 首先,小程序tabbar只识别list里面的东西

  2. 先在data中定义一个list和allList数组,把三重身份用户的list分别定义为list1,list2,list3,放入allList

  3. const roleId = wx.getStorageSync('statu')
    获取用户信息存到缓存中,根据不同和的roleId来判断是什么身份,

  4. this.setData({ list: this.data.allList[0].list2 })
    根据身份把allList里面的子数组赋值给系统默认识别的`list``

  5. switchTab的作用根据不同的路径切换tabbar下标

   switchTab(e) {      const data = e.currentTarget.dataset      const url = data.path      wx.switchTab({ url })      this.setData({        selected: data.index      })    }

index.json

{  "usingComponents": {}}

index.wxml

            {{item.text}}  

index.wxss

.tab-bar {  position: fixed;  bottom: 0;  left: 0;  right: 0;  height: 48px;  background: white;  display: flex;  padding-bottom: env(safe-area-inset-bottom);}.tab-bar-border {  background-color: rgba(0, 0, 0, 0.33);  position: absolute;  left: 0;  top: 0;  width: 100%;  height: 1px;  transform: scaleY(0.5);}.tab-bar-item {  flex: 1;  text-align: center;  display: flex;  justify-content: center;  align-items: center;  flex-direction: column;}.tab-bar-item .cover-image {  width: 44rpx;  height: 44rpx;}.tab-bar-item .cover-view {  margin-top: 8rpx;  font-size: 24rpx;}

最后,在tabbar里面设置过 pagePath的路径文件下的 xxx.js的onshow:function(){}里面设置

或者说凡是用到tabbar组件的页面对应的xx.js里的onshow:function(){}都要按照以下进行设置

不然会出现tabbar显示与点击不同步的现象

  /**   * 生命周期函数--监听页面显示   */  onShow: function () {    if (typeof this.getTabBar === 'function' &&      this.getTabBar()) {      this.getTabBar().setData({        selected: 0      })    }  },//selected: 0就是选中的tabbar下标,根据不同页面显示不同tabbar下标

结果展示

1.普通用户

2.运维人员

3.领导

可以看到根据用户信息里的roleId成功的切换了不同的tabbar

以上就是"微信小程序怎么根据不同用户切换不同TabBar"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。

不同 用户 身份 程序 个人 切换 我要 故障 知识 篇文章 下标 全局 路径 页面 维修 人员 信息 内容 就是 报表 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网黑科技 伟宏互联网科技 互联网下的科技发展 互联网金融 金融科技 科技类的互联网平台 济南华为互联网科技产业总部基地 小米互联网科技园设置 金融科技与互联网金融相同和不同 互联网校园科技培训 互联网和黑科技的看法 荆州市昕露互联网络科技有限公司 恒生互联网科技指数跳水 想开互联网科技公司怎么样 互联网企业科技类企业如何做账 美国互联网科技公司 互联网安防科技感 上海同邻互联网科技有限公司规模 10位科技互联网大佬 科技互联网专业务虚会材料 六子台科技互联网展会 美国互联网科技巨头监管 新华互联网科技校企合作 互联网科技公司招聘 乌鲁木齐新华智原互联网科技学校 黑科技 体验场景 互联网大会 互联网企业科技类企业如何做账 东营紫格互联网科技 萌小萌互联网科技河南有限公司 互联网科技创新方面的资料 科技互联网企业在行动
0