uniapp动态修改tabBar内容

uniapp根据角色权限动态改变tabBar内容

本人小白,如果有不对的地方请多指教

根据uniapp官方API进行修改  uni.setTbaBarItem()

解决的关键:

let tabBarOptions = {
    index: 0,  //要修改的下标
    text: '首页',  //文字
    iconPath: 'static/images/index_like.png',
    selectedIconPath: 'static/images/index_like_c.png'
    };
    //uniapp的api,传入上面写好的对象
  uni.setTabBarItem(tabBarOptions);

示例代码:

<template>
  <view>
	  
    <merchant v-if="curUserType == 2 || curUserType == 3"></merchant>
    <admin v-if="curUserType == 1"></admin>
	<Consumer v-if="curUserType == 4"></Consumer>
	
  </view>
</template>
<script>
import merchant from "./merchant";
import admin from "./admin";
// 引入消费者的首页组件
import Consumer from "./consumer";
import { mapGetters } from "vuex";
export default {
  components: {
    merchant,
    admin,
	Consumer
  },
  computed: {
    ...mapGetters(["curUserType"]),  //获取商户类型
  },
  created(){
	console.log(this.curUserType) 
	 this.curUserTabBar()
  },
  methods:{
	  curUserTabBar(){
		  // 判断如果是管理员,就使用以下的tabBar
		  if(this.curUserType==1){
			  // tabBar的样式
			  uni.setTabBarStyle({
				color: '#FFF',
				selectedColor: '#007AFF',  //选中的颜色
				backgroundColor: '#000000',
				borderStyle: 'black'
			});
			  let tabBarOptions = {
			  	index: 0,  //要修改的下标
			  	text: '商户地图',  //文字
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  let tabBarOptions1 = {
			  	index: 1,
			  	text: '附近商户',
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  let tabBarOptions2 = {
			  	index: 2,
			  	text: '监督检查',
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  // 将第一个修改
			  uni.setTabBarItem(tabBarOptions);
			  // 将第二个修改
			  uni.setTabBarItem(tabBarOptions1);
			  // 将第三个修改
			  uni.setTabBarItem(tabBarOptions2);
		  }else if(this.curUserType==2){  //判断如果等于2就是学校端
			 // tabBar的样式
			   uni.setTabBarStyle({
			 	color: '#FFF',
			 	selectedColor: '#007AFF',  //选中的颜色
			 	backgroundColor: '#000000',
			 	borderStyle: 'black'
			 });
			  let tabBarOptions = {
			  	index: 0,  //要修改的下标
			  	text: '附近商户',  //文字
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  let tabBarOptions1 = {
			  	index: 1,  //要修改的下标
			  	text: '商户地图',  //文字
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  let tabBarOptions2 = {
			  	index: 2,  //要修改的下标
			  	text: '个人中心',  //文字
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  // 将第一个修改
			  uni.setTabBarItem(tabBarOptions);
			  // 将第二个修改
			  uni.setTabBarItem(tabBarOptions1);
			  // 将第三个修改
			  uni.setTabBarItem(tabBarOptions2);
		  }else if(this.curUserType==3){  //判断如果等于3就是商户端
			 // tabBar的样式
			   uni.setTabBarStyle({
			 	color: '#FFF',
			 	selectedColor: '#007AFF',  //选中的颜色
			 	backgroundColor: '#000000',
			 	borderStyle: 'black'
			 });
			  let tabBarOptions = {
			  	index: 0,  //要修改的下标
			  	text: '附近商户',  //文字
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  let tabBarOptions1 = {
			  	index: 1,  //要修改的下标
			  	text: '商户地图',  //文字
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  let tabBarOptions2 = {
			  	index: 2,  //要修改的下标
			  	text: '个人中心',  //文字
			  	iconPath: 'static/images/index_like.png',
			  	selectedIconPath: 'static/images/index_like_c.png'
			  };
			  // 将第一个修改
			  uni.setTabBarItem(tabBarOptions);
			  // 将第二个修改
			  uni.setTabBarItem(tabBarOptions1);
			  // 将第三个修改
			  uni.setTabBarItem(tabBarOptions2);
			 
		  }else if(this.curUserType==4){  //判断如果等于4就是消费者端
			  uni.setTabBarStyle({
					color: 'yellow',
					selectedColor: '#007AFF',  //选中的颜色
					backgroundColor: 'red',
					borderStyle: 'black'
				});
				let tabBarOptions = {
					index: 0,  //要修改的下标
					text: '附近商户',  //文字
					iconPath: 'static/images/index_like.png',  //图标
					selectedIconPath: 'static/images/index_like_c.png'  
				};
				let tabBarOptions1 = {
					index: 1,
					text: '商户地图',
					iconPath: 'static/images/index_like.png',
					selectedIconPath: 'static/images/index_like_c.png'
				};
				let tabBarOptions2 = {
					index: 2,
					text: '个人中心',
					iconPath: 'static/images/index_like.png',
					selectedIconPath: 'static/images/index_like_c.png'
				};
				// 将第一个修改
				uni.setTabBarItem(tabBarOptions);
				// 将第二个修改
				uni.setTabBarItem(tabBarOptions1);
				// 将第三个修改
				uni.setTabBarItem(tabBarOptions2);

		  }
	  }
  }
  
};
</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值