【uniapp】微信小程序uniapp自定义底部导航栏

底部导航栏自定义

展示

在这里插入图片描述
该导航栏是基于微信导航栏的tabbar进行自定义的导航栏
自定义组件,组件中可以用微信的 cover-view代替
点击按钮触发uni.switchTab根据page.json中的url进行tabbar跳转
我这个目前是引用的自定义tabar组件,中间按钮凸出显示(多次切换有闪屏)这个,然后修改了他的bug,升级成为的。
不过也有问题,第一次切换会进行加载,tabbar会有闪,后面再进行切换便没有了。
如果有用希望点赞,如果有问题或者建议欢迎大家联系我,进行讨论。

主要文件目录

在这里插入图片描述

tabbar文件

<template>
	<view class="tab-block">
		<ul
			class='tab-list flex flex-center'
			:class="showMiddleButton === true?'tab-list-middle':'tab-list-default'"
		>
			<li
				v-for="(item, index) in tabList"
				:class="'list-item flex flex-column flex-middle ' + item.middleClass"
				@click="handlePush(item, index)"
				:key="index"
			>
				<view class="item-img-box">
					<image
						class="item-img"
						:src="tabIndex == index ? item.selectedIconPath : item.iconPath"
					/>
				</view>
				<view class="item-text font-20 color-black"
					:class="{ specialColor: tabIndex == index}"
				>
					{{item.text}}
				</view>
			</li>
		</ul>
		
		<!-- 兼容iPhonex下面的小黑条 -->
		<view class="tab-bar" v-show="showTabBar === true"></view>
	</view>
</template>

<script>
	export default {
		props: {
			tabIndex: { //当前选中的tab项
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				/*
				 * iconPath: 默认icon图片路径
				 * selectedIconPath: 选中icon图片路径 
				 * text: tab按钮文字
				 * pagePath:页面路径
				 * middleClass:该按钮所有的特殊样式类名
				 * tabList最小两项,最多五项
				 * tabList长度为奇数时,中间按钮才会突出显示
				 * 
				 */
				tabList: [{
						iconPath: '/static/tabbar/index.png',
						selectedIconPath: "/static/tabbar/index_a.png",
						text: '首页',
						pagePath: '/pages/index/index',
						middleClass: ''
					},
					{
						iconPath: '/static/tabbar/activity.png',
						selectedIconPath: '/static/tabbar/activity_a.png',
						text: '活动',
						pagePath: '/pages/activity/activity',
						middleClass: ''
					},
					{
						iconPath: '/static/tabbar/tabbar-logo.png',
						selectedIconPath: '/static/tabbar/tabbar-logo.png',
						text: '商城',
						pagePath: '/pages/shop/shop',
						middleClass: ''
					},
					{
						iconPath: '/static/tabbar/shopCar.png',
						selectedIconPath: '/static/tabbar/shopCar_a.png',
						text: '购物车',
						pagePath: '/pages/shopCar/shopCar',
						middleClass: ''
					},
					{
						iconPath: '/static/tabbar/mine.png',
						selectedIconPath: '/static/tabbar/mine_a.png',
						text: '我的',
						pagePath: '/pages/mine/mine',
						middleClass: ''
					}
				],
				showTabBar: false,
				showMiddleButton: false,
			};
		},
		computed: {
			getHeight() {
				return Number(this.height);
			},
		},
		mounted() {
			this.getSystemInfo()
			this.setTabBar()
		},
		methods: {
			//判断中间按钮是否突出显示,奇数or偶数,奇数突出
			setTabBar(){
				let tabLength = this.tabList.length
				if (tabLength % 2) {
					debugger
					this.showMiddleButton = true
					// 向上取整
					let middleIndex = Math.floor(tabLength / 2)
					// 给中间的添加mid-button
					this.tabList[middleIndex].middleClass = 'mid-button'
				}
			},
			
			//点击按钮
			handlePush(item, index) {
				if (this.tabIndex !== index) {
					uni.switchTab({
					    url: item.pagePath
					})	
				}
			},
			
			//兼容iPhoneX以上底部黑线条的显示
			getSystemInfo() {
				//获取系统信息
				uni.getSystemInfo({
					success: (res) => {
						// X及以上的异形屏top为44,非异形屏为20
						if (res.safeArea.top > 20) {
							this.showTabBar = true
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.specialColor{
		color: rgb(229, 113, 1);
	}
	.flex {
		display: flex;
		flex-flow: row wrap;
	}

	.flex-center {
		align-items: center;
		justify-content: center;
	}

	.flex-column {
		flex-direction: column;
	}

	.flex-middle {
		align-items: center;
	}
	.font-20 {
		font-size: 20rpx;
	}
	.tab-block {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 999;
		background-size: contain;
		width: 100vw;
		.tab-list{
			height:100rpx;
		}
		.tab-list-default{
			background-color: #FFFFFF;
			border-top: 1px #dddddd solid;
		}
		.tab-list-middle {
			position: relative;
			background: url('https://res.paquapp.com/popmartvip/home/nav_bar_bg_2x.png') no-repeat center center;
			background-size: cover;
		}
		.list-item {
			flex: 1;
			.item-img-box {
				width: 44rpx;
				height: 42rpx;
				margin-bottom: 9rpx;
				position: relative;
			}

			.item-img {
				width: 44rpx;
				height: 42rpx;
			}
		}

		.mid-button {
			position: relative;

			.item-img-box {
				width: 106rpx;
				height: 106rpx;
				margin-bottom: 9rpx;
				position: absolute;
				z-index: 1002;
				top: -104rpx;
			}

			.item-img {
				width: 106rpx;
				height: 106rpx;
			}

			.item-text {
				font-size: 20rpx;
				position: absolute;
				z-index: 1002;
				bottom: -40rpx;
			}
		}

		.tab-bar {
			height: 30rpx;
			background-color: #FFFFFF;
		}
	}
</style>

在page.json中定义tabbar

一般添加了tabbar,微信会根据渲染出,记得在App.vue中用uni.hideTabBar将其隐藏

	"tabBar": {
	        // "selectedColor":"#79D5AD",
	        // "color": "#999999",
	        // "backgroundColor":"#ffffff",
	        // "borderStyle": "white",
	        // "height":"0px",
	        "list": [{
	            "pagePath":"pages/index/index",
	            "text": "首页"
	        },{
	            "pagePath":"pages/activity/activity",
	            "text": "活动"
	        },{
	            "pagePath":"pages/shop/shop",
	            "text": "商城"
	        },{
	            "pagePath":"pages/shopCar/shopCar",
	            "text": "购物车"
	        },{
	            "pagePath":"pages/mine/mine",
	            "text": "我的"
	        }]
	}

在这里插入图片描述

在main.js中将组建进行全局注册

//挂载tabbar组件于全局
import Tabbar from '@/components/tabbar/tabbar.vue'
Vue.component('view-tabbar', Tabbar)

在这里插入图片描述

在页面中引用组建

tabIndex为当前在组建中list的位置
在这里插入图片描述

<think>好的,用户问的是在Uniapp开发的微信小程序中,只有首页有底部导航栏问题。我需要先回想一下Uniapp底部导航栏配置方式。通常,导航栏是在pages.json里通过tabBar设置的,里面的list数组定义了各个tab页。如果只有首页显示,可能的原因有几个: 首先,用户可能只在首页的页面路径配置了tabBar,而其他页面没有加入到list里。这时候需要检查每个需要底部导航的页面是否都正确添加到了tabBar的list中。 其次,可能是页面跳转的方式不对。比如,使用uni.navigateTo跳转的话,新页面会覆盖当前页,不会显示tabBar。正确的跳转方式应该是用uni.switchTab,这样才会切换到tab页并显示底部导航。需要提醒用户检查跳转方法是否正确。 另外,可能用户自定义导航栏,覆盖了原生的tabBar。这时候需要检查是否在pages.json中某个页面设置了"navigationStyle":"custom",导致原生导航栏被隐藏,从而影响了tabBar的显示。 还有,检查tabBar的配置是否正确,比如selectedColor、backgroundColor等是否有误,或者某些配置项缺失,导致部分页面不显示。虽然这种情况比较少见,但也不能排除。 用户可能遇到的情况还有:只有首页在tabBar的list里,其他页面没有添加进去。比如,list里只有一个首页的配置,所以自然只有首页有底部导航。这时候需要用户确认list数组里是否包含了所有需要显示底部导航的页面路径。 此外,可能用户在不同的页面有不同的显示逻辑,比如在onShow生命周期里动态隐藏tabBar。这时候需要检查页面代码是否有相关逻辑。 总结下来,解决问题的步骤应该是:检查pages.json中的tabBar配置,确认所有需要导航的页面都在list里;检查页面跳转的方法是否正确使用switchTab;检查是否有自定义导航栏的设置;最后,检查代码中是否有动态隐藏tabBar的逻辑。 可能需要用户提供部分代码,比如pages.json的tabBar配置和页面跳转的代码片段,以便更准确地定位问题。同时,提醒用户注意路径的大小写问题微信小程序对路径大小写敏感,是否配置的路径与实际路径一致。 还有,可能用户使用了条件编译,只在首页配置了tabBar,而其他平台或环境下没有正确配置。这时候需要检查条件编译的写法是否正确,是否影响到了其他页面的配置。 总之,需要从配置、跳转方法、自定义设置等多个方面逐一排查,找出导致只有首页显示底部导航的原因,并提供相应的解决方案。</think>在 UniApp 中,微信小程序底部导航栏(TabBar)是通过 `pages.json` 文件配置的。如果只有首页显示底部导航栏,通常是因为以下原因导致的: --- ### **1. 检查 `pages.json` 中的 `tabBar` 配置** 底部导航栏需要明确配置在 `pages.json` 中,且每个需要显示 TabBar 的页面必须添加到 `list` 数组中。 **错误示例**:仅首页配置了 TabBar,其他页面未添加: ```json { "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页" } // 其他页面未配置,导致不显示 TabBar ] } } ``` **正确配置**:所有需要显示 TabBar 的页面都应添加到 `list`: ```json { "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/user/user", // 其他页面路径 "text": "我的" } ] } } ``` --- ### **2. 页面跳转方式错误** 如果从首页跳转到其他页面时使用了 `uni.navigateTo` 或 `uni.redirectTo`,新页面会以“覆盖”形式打开,**不会显示底部 TabBar**。 **正确做法**:使用 `uni.switchTab` 跳转 TabBar 页面: ```javascript // 正确跳转方式(显示 TabBar) uni.switchTab({ url: '/pages/user/user' }); // 错误跳转方式(隐藏 TabBar) uni.navigateTo({ url: '/pages/user/user' }); ``` --- ### **3. 检查页面路径是否匹配** 确保 `list` 中的 `pagePath` 与实际页面路径完全一致(包括大小写)。微信小程序对路径大小写敏感。 --- ### **4. 排除自定义导航栏的干扰** 如果某个页面设置了自定义导航栏(`"navigationStyle": "custom"`),可能导致 TabBar 显示异常。尝试暂时关闭自定义导航栏进行测试。 --- ### **解决方案总结** 1. **配置所有 TabBar 页面**:在 `pages.json` 的 `tabBar.list` 中添加所有需要显示底部导航的页面。 2. **使用正确的跳转方式**:跳转 TabBar 页面时使用 `uni.switchTab`。 3. **检查路径一致性**:确保 `pagePath` 与实际路径完全匹配。 如果问题依旧,可以提供 `pages.json` 的 `tabBar` 配置和页面跳转代码片段,以便进一步排查!
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值