uniapp tabBar 中设置“custom“: true 在H5和app中无效解决办法

uniapp小程序自定义底部tabbar,但是在转成H5和app时发现"custom": true 无效,原生tabbar会显示出来
在这里插入图片描述
解决办法如下
在tabbar的list中设置 “visible”:false


代码如下:

"tabBar": {
		"custom": true,
		//"custom"仅对小程序生效
		"color": "#333",
		"fontSize": "12",
		"selectedColor": "#3F8CFF",
		"borderStyle": "black",
		"backgroundColor": "#fff",
		"list": [{
				"pagePath": "pages/index/index",
				"iconPath": "/static/tabbar/index.png",
				"selectedIconPath": "/static/tabbar/index-active.png",
				"text": "首页",
				"visible":false
				//"visible"仅对H5
			}, {
				"pagePath": "pages/workbench/index",
				"iconPath": "/static/tabbar/workbench.png",
				"selectedIconPath": "/static/tabbar/workbench-active.png",
				"text": "工作台",
				"visible":false
			},
			{
				"pagePath": "pages/mine/index",
				"iconPath": "/static/tabbar/mine.png",
				"selectedIconPath": "/static/tabbar/mine-active.png",
				"text": "我的",
				"visible":false
			}
		]
	}


注意:在App.vue里一定要加上这段代码 uni.hideTabBar,否则在app打包后,会有原生tabbar的样式

<script>
	export default {
		onLaunch: function() {
			//一定要加上这段
			uni.hideTabBar({
			  animation: false // 是否使用动画效果
			});
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

tabbar组件样式代码如下:


<template>
	<!-- Tabbar 组件  -->
	<view>
		<view class="cu-bar" :style="{
			backgroundColor:tabBarObj.backgroundColor?tabBarObj.backgroundColor:'#fff'
		}">
			<view @click="cubarClick(item)" class="cu-bar-box" v-for="(item,index) in tabBarList" :key="index">
				<view v-if="item.text !='工作台'">
					<!-- 有站内消息在我的右上角提示 -->
					<!-- <view v-if="item.text =='我的'" class="tishiBox">
					</view> -->
					<view class="">
						<image v-if="pageCur ==item.pagePath" class="cu-bar-icon" :src="item.selectedIconPath" mode="">
						</image>
						<image v-else class="cu-bar-icon" :src="item.iconPath" mode=""></image>
					</view>
					<view :style="{
					color:pageCur ==item.pagePath?tabBarObj.selectedColor:tabBarObj.color,
					fontSize:tabBarObj.fontSize+'px'
				}">
						{{item.text||'-'}}
					</view>
				</view>
				<view class="yuanBox" v-else>
					<view class="yuanTop"></view>
					<view class="yuanjuli"></view>
					<view class="yuan" :class="{yuanActive:pageCur ==item.pagePath}">
						<image v-if="pageCur ==item.pagePath" class="cu-bar-icon yuan-cu-bar-icon"
							:src="item.selectedIconPath" mode="">
						</image>
						<image v-else class="cu-bar-icon yuan-cu-bar-icon" :src="item.iconPath" mode="">
						</image>
					</view>
				</view>
			</view>
		</view>
		<view :style="{
			height:cardHeight+'px'
		}">
			<!-- 底部占位使用,内容超出页面高度产生滚动条时,tabbar不会遮盖引用页面内容 -->
		</view>
	</view>
</template>
<script>
	import pagesJson from '@/pages.json'; // 引用配置文件的tabbar配置信息
	export default {
		props: {
			pageCur: {
				type: String,
				default: 'pages/index/index' //默认为首页
			}, //  tabbar当前页面
		},
		data() {
			return {
				tabBarObj: pagesJson.tabBar, //tabbar全部配置
				tabBarList: pagesJson.tabBar.list, //tabbar列表
				cardHeight: 0, //tabbar高度
			}
		},
		onLoad() {
		},
		onShow: function() {},
		mounted() {
			let than = this
			this.createIntersectionObserver().relativeToViewport().observe('.cu-bar', (res) => {
				than.cardHeight = res.intersectionRect.height || 0
				// console.log("元素高度信息",res.intersectionRect.height)
				// res.id // 目标节点 id
				// res.dataset // 目标节点 dataset
				// res.intersectionRatio // 相交区域占目标节点的布局区域的比例
				// res.intersectionRect // 相交区域
				// res.intersectionRect.left // 相交区域的左边界坐标
				// res.intersectionRect.top // 相交区域的上边界坐标
				// res.intersectionRect.width // 相交区域的宽度
				// res.intersectionRect.height // 相交区域的高度
			})
		},
		methods: {
			cubarClick(item) {
				// console.log("点击的元素",item)
				uni.switchTab({
					url: '/' + item.pagePath
				});
			}
		}
	}
</script>

<style scoped>
	.cu-bar {
		display: flex;
		width: 100vw;
		padding-bottom: 40rpx;
		padding-top: 10rpx;
		position: fixed;
		bottom: 0px;
		left: 0px;
		right: 0px;
		box-shadow: 0 -1rpx 6rpx rgba(0, 0, 0, 0.1);
	}

	.cu-bar .cu-bar-box {
		flex: 1;
		text-align: center;
		position: relative;
	}

	.cu-bar .cu-bar-icon {
		width: 50rpx;
		height: 50rpx;
		display: inline-block;
	}

	.yuanBox {
		position: relative;
		background: pink;
		text-align: center;
	}

	.yuanTop {
		height: 80rpx;
		background-color: #fff;
		border-radius: 78rpx 78rpx 0rpx 0rpx;
		border-bottom: 0rpx;
		width: 160rpx;
		margin: 0 auto;
		box-shadow: 0 -1rpx 6rpx rgba(0, 0, 0, 0.1);
		position: absolute;
		top: -35rpx;
		left: 50%;
		margin-left: -80rpx;
	}

	.yuanjuli {
		height: 60rpx;
		margin-top: 0rpx;
		background: #fff;
		position: absolute;
		width: 100%;
		top: -10rpx;
	}

	.yuan {
		display: inline-flex;
		background: #fff;
		border: 2rpx solid #ccc;
		width: 96rpx;
		height: 96rpx;
		padding: 14rpx;
		box-sizing: border-box;
		border-radius: 50%;
		align-items: center;
		justify-content: center;
		overflow: hidden;
		position: absolute;
		left: 50%;
		margin-left: -48rpx;
		top: -14rpx;

	}

	.yuan .yuan-cu-bar-icon {
		width: 100%;
		height: 100%;
	}

	.yuanActive {
		background: #3F8CFF;
		border: 2rpx solid #3F8CFF;
	}

	.tishiBox {
		width: 20rpx;
		height: 20rpx;
		border-radius: 50%;
		background-color: #ff3737;
		position: absolute;
		left: 60%;
		top: 0rpx;
	}
</style>

引用页面代码如下:

<tabbarBox pageCur="pages/index/index" />
//注意:pageCur  引用tabbar页面地址是什么,这里就传什么   tabbar组件跳转时使用

<script>
	import tabBox from '@/components/tabBox.vue';
	export default {
		components: {
			tabbarBox
		},
		
	}
</script>
### 如何在 UniAppH5 平台中实现自定义 Tabbar #### 1. 修改 `manifest.json` 文件配置 为了启用自定义 tabBar,在项目的根目录下找到并编辑 `manifest.json` 文件。进入 **"App模块配置" -> "自定义导航栏底部标签栏"**,开启 `custom-tab-bar` 字段,并将其指向一个 Vue 组件路径[^1]。 以下是具体的配置示例: ```json { "plus": { "tabBar": { "custom": true, "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/cart/cart", "text": "购物车" } ] } } } ``` 注意:H5 平台上默认不会显示原生 tabBar,因此无需调用 `uni.hideTabBar()` 方法来隐藏它[^3]。 --- #### 2. 创建自定义 Tabbar 组件 创建一个新的 Vue 文件作为自定义 tabBar 的组件文件,例如命名为 `mytab.vue`。此组件将负责渲染所有的 tab 列表项以及处理点击事件跳转逻辑[^2]。 下面是该组件的一个简单实现例子: ```vue <template> <view class="tab-bar"> <block v-for="(item, index) in list" :key="index"> <view class="tab-item" :class="{ active: current === item.pagePath }" @click="switchPage(item)" > {{ item.text }} </view> </block> </view> </template> <script> export default { data() { return { list: [ { pagePath: "/pages/index/index", text: "首页" }, { pagePath: "/pages/cart/cart", text: "购物车" } ], current: "" }; }, onLoad(options) { this.current = options.path || "/"; }, methods: { switchPage(item) { const url = encodeURIComponent(item.pagePath); if (this.current !== item.pagePath) { uni.redirectTo({ url: `/pages/mytab/mytab?path=${url}` }); } } } }; </script> <style scoped> .tab-bar { display: flex; justify-content: space-around; align-items: center; height: 50px; background-color: #f8f8f8; } .tab-item { padding: 10px; font-size: 14px; } .active { color: blue; } </style> ``` 上述代码实现了以下功能: - 动态加载指定的页面列表。 - 当前选中的 tab 使用样式高亮标记。 - 跳转到对应的目标页面时传递参数以便更新当前状态。 --- #### 3. 页面间通信机制 为了让每个页面都能知道当前处于哪个 tab 下面的状态,可以通过 URL 参数或者 Vuex 来共享数据。这里采用的是通过 URL 查询字符串的方式获取当前激活的路由地址。 例如,在主应用入口处监听路径变化从而同步至子组件: ```javascript onLoad(option) { console.log('Current Path:', decodeURIComponent(option.path)); this.current = decodeURIComponent(option.path); // 更新当前活动页索引 } ``` 这样可以确保无论何时切换回某个特定页面都会自动刷新其关联的菜单选项卡位置。 --- #### 4. 注意事项 尽管可以在 H5 中完全依赖前端技术来自由定制外观设计,但仍需考虑兼容性性能优化问题。比如减少不必要的 DOM 元素数量、控制图片资源大小等措施都可以提升用户体验质量。 另外值得注意的一点是,如果涉及到多端适配开发,则可能还需要额外调整部分样式属性以满足不同平台的要求差异性。 --- ### 总结 综上所述,通过修改 manifest 配置引入外部组件形式即可轻松完成整个流程操作;同时借助简单的 JavaScript 控制流管理界面交互行为,最终达成预期目标——即制作一款高度灵活可扩展性的跨屏解决方案!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值