uniapp动态设置tabbar

1、将所有需要的tabbar配置在pages.json里 
"tabBar": {
		"color": "#BEC0C3",
		"selectedColor": "#00A29C",
		"borderStyle": "#EEEEEE",
		"backgroundColor": "#ffffff",
		"list": [{
				"pagePath": "pages/tabBar/devicePage",
				"iconPath": "static/image/tabbar/device.png",
				"selectedIconPath": "static/image/tabbar/device_active.png",
				"text": "设备"
			},
			{
				"pagePath": "pages/tabBar/voicePage",
				"iconPath": "static/image/tabbar/device.png",
				"selectedIconPath": "static/image/tabbar/device_active.png",
				"text": "语音"
			},

			{
				"pagePath": "pages/tabBar/warnPage",
				"iconPath": "static/image/tabbar/warning.png",
				"selectedIconPath": "static/image/tabbar/warning_active.png",
				"text": "预警"
			}, {
				"pagePath": "pages/tabBar/locationPage",
				"iconPath": "static/image/tabbar/position.png",
				"selectedIconPath": "static/image/tabbar/position_active.png",
				"text": "位置"
			}, {
				"pagePath": "pages/tabBar/fencePage",
				"iconPath": "static/image/tabbar/fence.png",
				"selectedIconPath": "static/image/tabbar/fence_active.png",
				"text": "围栏"
			}, {
				"pagePath": "pages/tabBar/myPage",
				"iconPath": "static/image/tabbar/my.png",
				"selectedIconPath": "static/image/tabbar/my_active.png",
				"text": "我的"
			}
		]
	},

2、app.vue 的globalData里加入reviseTabbarByUserType方法

globalData: {
		reviseTabbarByUserType: function () {
			let isAdmin = uni.getStorageSync('isAdmin');
			if (isAdmin == 1) {
				uni.setTabBarItem({
					index: 1,
					visible: false,
				})
			} else {
				uni.setTabBarItem({
					index: 0,
					visible: false,
				})
			}
		}
	},

3、涉及到的所有tabbar界面在onshow里调用reviseTabbarByUserType方法

onShow() {
		getApp().globalData.reviseTabbarByUserType();
	},

###补充reviseTabbarByUserType方法解释

在uniapp中,reviseTabbarByUserType这个方法是用来根据用户类型修改TabBar的展示内容

  reviseTabbarByUserType(userType) {
    const tabbar = {
      list: [],
    };
    
    // 根据用户类型设置TabBar列表
    if (userType === 'admin') {
      tabbar.list = [
        {
          pagePath: '/pages/home/home',
          text: '首页',
        },
        {
          pagePath: '/pages/admin/admin',
          text: '管理',
        },
      ];
    } else {
      tabbar.list = [
        {
          pagePath: '/pages/home/home',
          text: '首页',
        },
        {
          pagePath: '/pages/user/user',
          text: '我的',
        },
      ];
    }
    
    // 修改TabBar
    uni.setTabBarItem(tabbar);
  }

     

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值