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" />
相关推荐
Cerrda9 分钟前
🌟让你的uniapp应用拥有更现代的交互体验,一个支持滚动渐变透明的导航栏组件🌟
uni-app
2501_916007471 小时前
iOS 应用性能测试的工程化流程,构建从指标采集到问题归因的多工具协同测试体系
android·ios·小程序·https·uni-app·iphone·webview
book多得3 小时前
刷题专用微信小程序推荐
微信小程序·小程序
00后程序员张5 小时前
iOS 抓不到包怎么办?从 HTTPS 解密、QUIC 排查到 TCP 数据流分析的完整解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
前端互助会5 小时前
UNI-APP开发APP避坑指南:这些关键事项你必须掌握
uni-app
技术与健康6 小时前
微信小程序云开发实践:共享环境与LLM整合经验
微信小程序·小程序
老华带你飞7 小时前
社区养老保障|智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·社区养老保障
发财北8 小时前
本地生活小程序开发方案
小程序
游戏开发爱好者821 小时前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
toooooop81 天前
Vuex 中 state、mutations 和 actions 的原理和写法
前端·javascript·uni-app