uniapp小程序使用自定义底部tabbar,并根据用户类型动态切换tabbar数据

1.注意点

在pages.json中配置tabbar如下字段:custom:true ,会自动隐藏原生tabbar,使用自定义的tabbar

2.如何自定义呢

  • 可以使用第三方组件库的tabbar组件,然后二次封装下内部封装逻辑:
    1.点击切换逻辑
    2.根据用户类型,动态切换tabbar数据

3.具体逻辑实现,代码如下

(1)封装的自定义tabbar组件如下:

javascript 复制代码
<template>
	<tui-tabbar :current="propIndex" backgroundColor="#fff" :tabBar="tabBar" color="#000" selectedColor="#FF7D0D"
		@click="tabbarSwitch" :unlined="true"></tui-tabbar>
</template>

<script>
	import {
		globalTabbarData
	} from '@/common/utils.js'
	const app = getApp()
	export default {
		data() {
			return {
				tabBar: globalTabbarData
			}
		},
		props:{
			propIndex: { // 父组件传入激活的tab索引
				default: 0
			}
		},
		created() {
			const userInfo = uni.getStorageSync('userInfo')
			// 用户类型
			if(!userInfo.userType) { // 普通用户
				this.tabBar = globalTabbarData.slice(2)
			} else { // 发布者
				this.tabBar = globalTabbarData.slice(0, 2)
			}
		},
		mounted() {
			uni.switchTab({
				url:  '/' + this.tabBar[this.propIndex].pagePath
			})
		},
		methods: {
			tabbarSwitch(e) {
				uni.switchTab({
					url:  '/' + this.tabBar[this.current].pagePath
				})
			},
		}
	}
</script>

(2)组件使用

javascript 复制代码
<custom-tabbar :propIndex="1" />
相关推荐
游戏开发爱好者83 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106325 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106325 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息6 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”7 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
局外人LZ1 天前
Uniapp脚手架项目搭建,uniapp+vue3+uView pro+vite+pinia+sass
前端·uni-app·sass
光影少年1 天前
AIGC + Taro / 小程序
小程序·aigc·taro
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
前端呆头鹅1 天前
Websocket使用方案详解(uniapp版)
websocket·网络协议·uni-app
浮桥1 天前
uniapp+h5 公众号实现分享海报绘制
uni-app·notepad++