uni-app自定义tabBar

一个项目有多个角色,比如医生和患者,tabBar跳转的路径不一样,但是在pages.json中无法配置多个tabBar,这时候就要自定义tabBar了

下面一个小小demo,场景是医生和患者端,一共四个不同的页面,分别是医生首页,患者首页,医生我的页面,患者我的页面,tabbar要求根据不同的角色跳转到对应的路径

(一)配置pages.json

custom设置为true,并且把所有需要切换的页面都配置在list中,否则之后切换tab用switchTab方法无效

"tabBar": {
		"custom": true, 
		"color": "#333333",
		"selectedColor": "#4256A8",
		"borderStyle": "black",
		"backgroundColor": "#ffffff",
		"list": [{
				"pagePath": "pages/patientHome/patientHome",
				"iconPath": "static/images/home.png",
				"selectedIconPath": "static/images/home_s.png",
				"text": "首页"
			},
			{
				"pagePath": "pages/mine/mine",
				"iconPath": "static/images/mine.png",
				"selectedIconPath": "static/images/mine_s.png",
				"text": "我的"
			},
            {
				"pagePath": "pages/doctorDine/doctorDine",
				"iconPath": "static/images/mine.png",
				"selectedIconPath": "static/images/mine_s.png",
				"text": "我的"
			},
			{
				"pagePath": "pages/volunteerHome/volunteerHome",
				"iconPath": "static/images/mine.png",
				"selectedIconPath": "static/images/home_s.png",
				"text": "首页"
			},
			{
				"pagePath": "pages/pharmacyHome/pharmacyHome",
				"iconPath": "static/images/mine.png",
				"selectedIconPath": "static/images/home_s.png",
				"text": "首页"
			}
		]
	}

(二)tab-bar.vue 组件

<template>
	<view class="tab-bar">
		<view v-for="(item,index) in list" :key="index" class="tab-bar-item" @click="switchTab(item, index)">
			<image class="tab_img" :src="selected === index ? item.selectedIconPath : item.iconPath"></image>
			<view class="tab_text" :style="{color: selected === index ? selectedColor : color}">{{item.text}}</view>
		</view>
	</view>

</template>



<script>
	export default {
		props: {
			selected: { // 当前选中的tab index
				type: Number,
				default: 0
			},
			userIdentity: { // 当前角色
				type: Number,
				default: 0
			}

		},
		data() {
			return {
				color: "#333333",
				selectedColor: "#333333",
				list: [{
					pagePath: "/pages/patientHome/patientHome",
					iconPath: "/static/images/home.png",
					selectedIconPath: "/static/images/home_s.png",
					text: "首页"
				}, {
					pagePath: "/pages/mine/mine",
					iconPath: "/static/images/mine.png",
					selectedIconPath: "/static/images/mine_s.png",
					text: "我的"
				}]
			}
		},
		methods: {
			switchTab(item, index) {
				console.log("item", item)
				console.log("index", index)
				let url = item.pagePath;
                // 对应患者和医生的首页
				if (index == 0) {
					switch (this.userIdentity) {
						// 患者
						case 0:
							url = "/pages/patientHome/patientHome"
							break
						// 医生
						case 1:
							url = "/pages/doctorHome/doctorHome"
							break
					}

				}
                // 对应患者和医生的我的页面
				if (index == 1) {
					switch (this.userIdentity) {
						// 患者
						case 0:
							url = "/pages/mine/mine"
							break
						// 医生
						case 1:
							url = "/pages/doctorMine/doctorMine"
							break
					}

				}
				uni.switchTab({
					url
				})

			}
		}
	}
</script>

<style lang="scss">
	.tab-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 100rpx;
		background: white;
		display: flex;
		justify-content: center;
		align-items: center;
		padding-bottom: env(safe-area-inset-bottom); // 适配iphoneX的底部

		.tab-bar-item {
			flex: 1;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;

			.tab_img {
				width: 37rpx;
				height: 41rpx;
			}

			.tab_text {
				font-size: 20rpx;
				margin-top: 9rpx;
			}
		}
	}
</style>

(三)不同的角色调用tabBar

医生端:

<tab-bar :userIdentity="1"></tab-bar>

患者端:

<tab-bar :userIdentity="0"></tab-bar>

我的页面:

<tab-bar :selected="1"></tab-bar>

(四)最后效果:

不同角色下,点击的tab可以切换到对应的页面

 

组件还有很多可扩展的地方,因为demo中tabBar的差异仅是跳转路径不同,所以对index做判断从而做特殊处理即可,

还有一些不同页面tabBar差异较大的场景,可以考虑根据不同的角色获取不同的tabBar list

 

 

 

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值