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设为空数组。

相关推荐
GISer_Jing5 分钟前
React底层架构深度解析:从虚拟DOM到Fiber的演进之路
前端·react.js·架构
斯密码赛我是美女12 分钟前
ssti刷刷刷
java·服务器·前端
Mryan200524 分钟前
Angular | 利用 `ChangeDetectorRef` 解决 Angular 动态显示输入框的聚焦问题
前端·javascript·angular.js
郭尘帅66626 分钟前
Vue3中实现轮播图
开发语言·前端·javascript
众乐乐_20081 小时前
Java 后端给前端传Long值,精度丢失的问题与解决
java·前端·状态模式
一叶茶1 小时前
VsCode和AI的前端使用体验:分别使用了Copilot、通义灵码、iflyCode和Trae
前端·vscode·gpt·ai·chatgpt·copilot·deepseek
熊猫钓鱼>_>1 小时前
基于MCP的桥梁设计规范智能解析与校审系统构建实践
前端·easyui·设计规范
若初&1 小时前
文件上传Ⅲ
前端·web安全
若愚67921 小时前
前端取经路——前端安全:构建坚不可摧的Web应用防线
前端·安全