uniapp自定义底部tabBar

使用场景:在一个非tabbar页面,想要有底部导航效果,故自定义效果,系统原底部导航栏仍在正常使用

效果:

布局:

html 复制代码
<template>
	<view class="tab-bar" :style="{height: height + 'px'}">
		<view v-for="(item, index) in tabBars" :key="index" @click="switchTab(item)"
			:style="{color: index === selectIndex ? item.selectedTextColor : item.textColor}">
			<view style="display: flex; justify-content: center;">
				<image :src="index === selectIndex ? item.selectedIconPath : item.iconPath" style="height: 25px; width: 25px;"></image>
			</view>
			<view style="margin-top: 2px; font-size: 12px;">
				{{item.text}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"tabBar",
		props: {
			tabBars: {
				type: Array,
				required: true
			},
			selectIndex: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				height: undefined
			};
		},
		methods: {
			switchTab(item) {
				if (this.getCurrentPageIndex() !== item.pagePath) {
					uni.redirectTo({
						url: item.pagePath
					});
				}
			},
			getCurrentPageIndex() {
				const pages = getCurrentPages();
				return pages[pages.length - 1].route;
			}
		},
		mounted() {
			let systemInfo = uni.getSystemInfoSync();
			let tabBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight;
			this.height = tabBarHeight
		}
	}
</script>

<style>
.tab-bar {
  display: flex;
  justify-content: space-around;
  position: fixed;
  height: 5vh;
  bottom: 0;
  width: 100%;
  background-color: #fbfcfe;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  padding-top: 3px;
  padding-bottom: 2px;
}
</style>

使用:

html 复制代码
<tabBar :tab-bars="approvalTabbar" :selectIndex="1"></tabBar>
// selectIndex 是当前页面的数组下标
approvalTabbar = [
	{
		iconPath: '/static/icon/mess.png',
		selectedIconPath:'/static/icon/mess-selected.png',
		text: "发起申请",
		pagePath:'/pages/approval/launch/launch',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	},
	{
		iconPath: '/static/icon/approval-mine.png',
		selectedIconPath:'/static/icon/approval-mine-selected.png',
		text: "我审批的",
		pagePath:'/pages/approval/accraditation/accraditation',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	},
	{
		iconPath: '/static/icon/submit.png',
		selectedIconPath:'/static/icon/submit-selected.png',
		text: "已提交",
		pagePath:'/pages/approval/submit/submit',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	}
]

不足之处:

1.需要在参与跳转的每个页面引入组件

2.引入时需要传递相同的tabs数组数据、页面与数组对应的下标

3.页面跳转后会重绘底部导航栏,出现闪的问题

相关推荐
特立独行的猫a3 小时前
uni-app 开发HarmonyOS的鸿蒙影视项目分享:从实战案例到开源后台
uni-app·开源·harmonyos·鸿蒙·影视
七七小报6 小时前
uniapp-商城-61-后台 新增商品(添加商品到数据库)
uni-app
hbcui19846 小时前
uni-app x正式支持鸿蒙原生应用开发
uni-app·harmonyos·uni-app x
lqj_本人6 小时前
鸿蒙OS&UniApp制作支持多图上传的图片选择器:打造高性能移动端上传体验#三方框架 #Uniapp
华为·uni-app·harmonyos
好好的哦8 小时前
uni-app小程序登录后…
小程序·uni-app
!win !21 小时前
uni-app小程序登录后…
前端·uni-app
aklry1 天前
uniapp实现在线pdf预览以及下载
前端·pdf·uni-app
繁依Fanyi1 天前
我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
java·pdf·uni-app·生活·harmonyos·codebuddy首席试玩官
陌路物是人非1 天前
uniapp取消浏览自动填充
java·服务器·uni-app
lqj_本人1 天前
鸿蒙OS&UniApp实现视频播放与流畅加载:打造完美的移动端视频体验#三方框架 #Uniapp
uni-app·音视频·harmonyos