uniapp如何根据不同角色自定义不同的tabbar

思路:

1.第一种是根据登录时获取的不同角色信息,来进行 跳转到不同的页面,在这些页面中使用自定义tabbar

2.第二种思路是封装一个自定义tabbar组件,然后在所有要展示tabbar的页面中引入使用

1.根据手机号码一键登录,在回调中获取用户信息进行跳转

  /**
	   * @param {object} e 获取手机号码组件回调参数
	   * @description 家政人员一键登录组件回调
	   */
    async getPhoneNumber(e) {
      if (e.detail.errMsg == "getPhoneNumber:ok") {
        this.phoneCode = e.detail.code;
        if (this.loginCode == "") {
          await this.getCode();
        }
        this.loginForm = {
          loginCode: this.loginCode,
          phoneCode: this.phoneCode,
        };
        this.$http.staffWxLogin(this.loginForm).then(res => {
          if (res.code == 200) {
            console.log(res, 'res')
            uni.setStorageSync("token", res.data.token);
            uni.setStorageSync("employeeStaffId", res.data.userId);
            uni.setStorageSync('userType', 1);
			/**
			 * 家政端
			 */
            // uni.reLaunch({
            //   url: '/pages/bottomPage/index'
            // })
			/**
			 * 司机端
			 */
			uni.reLaunch({
				url: "/page-diver/diver/tabBar/tabBar"
			})
							
          }
        })
      }
    },

2.在tabbar页面中完成自定义tabbar,并完成根据激活的tabbar展示不同页面的逻辑

其实就是调用v-if来控制不同页面的显示

<template>
	<view style="height: 100vh; overflow-y: hidden;display: flex;flex-direction: column;">
		<view style="overflow-y: hidden;flex-grow: 10;">
			<!-- 首页 -->
			<scroll-view style="height: 100%;" v-if="currentTab === 'index'" scroll-y>
				<index-com></index-com>
			</scroll-view>
			<!-- 客户 -->
			<scroll-view style="height: 100%;" v-if="currentTab === 'customer'">
				<index-com></index-com>
			</scroll-view>
			<!-- 人员 -->
			<view style="height: 100%;" v-if="currentTab === 'person'" >
				<index-com></index-com>
			</view>
			<!-- 合同 -->
			<view style="height: 100%;" v-if="currentTab === 'contract'">
				<index-com></index-com>
			</view>
			<!-- 工具 -->
			<view style="height: 100%;" v-if="currentTab === 'tool'" scroll-y>
				<index-com></index-com>
			</view>
		</view>
		<!-- tabBar -->
		<u-tabbar :value="currentTab" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true"
			activeColor="#FF7993" inactiveColor="#A7A7A7" style="flex: 0">
			<u-tabbar-item  v-for="(item,index) in iconList"  :text="item.label" :key="index" :icon="item.isActive?item.active:item.path" 
			@click="barClick"></u-tabbar-item>
		</u-tabbar> 
	</view>
</template>

<script>
	import indexCom from '../index/index.vue'
	export default {
		components: {
			indexCom,
		
		},
		data() {
			return {
				currentTab: 'index',
				iconList: [
					{
						path:'/static/tabBar/diver-home.png',
						active:'/static/tabBar/diver-achome.png',
						name:'index',
						isActive:true,
						label:'首页'
					},
					{
						path:'/static/tabBar/diver-car.png',
						active:'/static/tabBar/diver-accar.png',
						name:'tool',
						isActive:false,
						label:'我的车队'
					},
					{
						path:'/static/tabBar/diver-my.png',
						active:'/static/tabBar/diver-acmy.png',
						name:'my',
						isActive:false,
						label:'我的'
					}
				]
			}
		},
		methods: {
			barClick(e,name){
				for(let i =0;i<this.iconList.length;i++){
					if(i === e){
						 if(!this.iconList[i].isActive){
							 this.iconList[i].isActive = true
							 this.currentTab =i
							 console.log(this.currentTab,'currentTab')
						 }
					}else{
							 this.iconList[i].isActive = false
						 }
				}
			}
		}
	}
</script>

<style>

</style>

第二张思路就不赘述了,直接用上面的tabbar封装成组件引用即可,主要在pages.json中将tabbarlist设为空数组。

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp中,可以通过自定义tabbar来实现不同身份显示不同tabbar。具体步骤如下: 1. 定义tabbar样式:在pages文件夹下创建一个名为"tabbar"的文件夹,并分别创建不同身份对应的tabbar页面,例如"tabbar/normal.vue"和"tabbar/vip.vue"。 2. 在App.vue中设置自定义tabbar:在App.vue的template中添加一个tabbar组件,并根据用户身份使用条件渲染来切换不同tabbar。例如: ``` <template> <view> <!-- 正常用户的tabbar --> <tabbar type="normal" v-if="userType === 'normal'"></tabbar> <!-- VIP用户的tabbar --> <tabbar type="vip" v-if="userType === 'vip'"></tabbar> </view> </template> ``` 3. 在App.vue的script中定义用户身份变量:在data中定义一个userType变量来表示用户的身份,并根据实际情况来设置默认值或从后台获取用户身份。例如: ``` export default { data() { return { userType: 'normal' // 默认为正常用户 } } } ``` 4. 在各个页面中使用tabbar:在各个页面的template中添加自定义tabbar组件,并在props中接收tabbar的type属性。例如: ``` <template> <view> <!-- 具体页面内容 --> </view> <!-- 自定义tabbar --> <tabbar :type="type"></tabbar> </template> <script> export default { props: { type: String // 接收tabbar的type属性 } } </script> ``` 这样就可以根据用户的身份来显示不同tabbar,实现不同身份显示不同tabbar的效果。需要注意的是,在自定义tabbar组件中,可以根据需求添加相应的功能和样式,以满足不同身份用户的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值