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" />
相关推荐
2501_9151063211 小时前
深入解析无源码iOS加固原理与方案,保护应用安全
android·安全·ios·小程序·uni-app·cocoa·iphone
万能小林子13 小时前
2026 AI开发新范式:Vibe Coding生成网页 + 3分钟打包成App,非技术人也能独立发布自己的App!
人工智能·uni-app·ai编程·web app·vibecoding
weikecms14 小时前
CPS返利小程序一键搭建教程
小程序
白菜__14 小时前
微信小程序网关逆向分析
javascript·微信小程序·小程序·node.js·网络爬虫·微信网关·小程序网关
TANKING-14 小时前
微信小程序订阅消息推送系统(一次性/长期订阅消息推送)
微信小程序·小程序
李白的天不白14 小时前
小程序not 404
小程序
我是伪码农14 小时前
小程序75-100
小程序
一颗小青松15 小时前
uniapp 集成友盟并且上传页面路径
前端·vue.js·uni-app
00后程序员张1 天前
HTTPS单向认证、双向认证、抓包原理与反抓包策略详解
网络协议·http·ios·小程序·https·uni-app·iphone