uniapp自定义权限菜单,动态tabbar

已封装为组件,亲测4个菜单项目可以切换,

以下为示例,根据Storage 中 userType 的 值,判断权限菜单

javascript 复制代码
<template>
	<view class="tab-bar pb10">
		<view class="tabli" v-for="(tab, index) in tabs" :key="index" @click="switchTab(tab)">
			<image :src="currentTab === tab.text ? tab.selectedIconPath : tab.iconPath" mode="aspectFit"></image>
            <text :class="currentTab === tab.text ? 'active' : ''">{{ tab.text }}</text>
		</view>
		<loginTourist ref="loginPop"></loginTourist>
	</view>
</template>
<script>
export default {
		props: {//当前页
			currentTab: {
				type: String,
				required: true
			}
		},
		data() {
			return {
				// 游客、管理员、村民
				usertype: uni.getStorageSync('userType'),
			}
		},
computed: {
			// 权限菜单
			tabs() {
				if (this.usertype =='村民') {
					return [
						{
							"pagePath": "/pages/homepage/index",
							"iconPath": this.img('home-1'),
							"selectedIconPath": this.img('home'),
							"text": "首页"
						},
{
							"pagePath": "/workPages/teach/index",
							"iconPath": this.img('bs-1'),
							"selectedIconPath": this.img('bs'),
							"text": "办事指南"
						},
                        {
							"pagePath": "/pages/messag/index",
							"iconPath": this.img('mass-1'),
							"selectedIconPath": this.img('mass'),
							"text": "消息"
						},
                        {
							"pagePath": "/pages/mine/index",
							"iconPath": this.img('mine-1'),
							"selectedIconPath": this.img('mine'),
							"text": "我的"
						}
                           ],
                    }else if (this.usertype =='管理员') {
					// 管理员
				 	    return [
				 		        {
							    "pagePath": "/pages/homepage/index",
							    "iconPath": this.img('home-1'),
							    "selectedIconPath": this.img('home'),
							    "text": "首页"
						        }, 
                            ],
                        }
                }
            },
methods: {
			switchTab(tab) {
				// console.log("底部导航", tab)
				let userType = uni.getStorageSync('userType')||this.userType
				let token = uni.getStorageSync('token')
				console.log('底部导航,用户,token|',tab.text ,userType,token)
				uni.navigateTo({
						url: tab.pagePath
					});
                },
            // 统一加图片域名路径
			img(img) {
				return 图片网址前缀 + 'tabBar/' + img + '.png'
			},
        },
			
</script>
<style>
	.tab-bar {
		justify-content: space-around;
		align-items: center;
		height: 150upx;
		position: fixed;
        left: 0;
		z-index: 99999999999999999999;
		width: 100%;
		display: flex;
		justify-content: space-around;
		background: rgb(240 242 245 / 97%);
		border-top: 0.5px solid rgba(240, 242, 245, 1)
		
	}

	.tab-bar .tabli {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
justify-content: center;
	}

	.tab-bar image {
		width: 25px;
		height: 25px;
	}

	.tab-bar text {
		font-size: 12px;
		margin-top: 5px;
	}

	.tab-bar text.active {
		font-weight: bold;
	}
</style>

声明为全局组件:main.js 中添加

javascript 复制代码
import tzTabBar from "@/components/atz-tabbar/atz-tabbar.vue"//自定义底部菜单
Vue.component('tzTabBar', tzTabBar)

在页面中使用:

javascript 复制代码
<tzTabBar :currentTab="'消息'"></tzTabBar>
相关推荐
涉密IT资质笔记40 分钟前
涉密人员脱密期管理规范:期限分级模型、就业限制边界与违规认定标准
java·服务器·前端·网络·数据库
u0111026751 小时前
工具页案例图如何编写 alt让图片说明与处理场景对应
java·前端·javascript·图像处理·人工智能·算法·ai作画
事圆则缓1 小时前
Flutter 状态管理框架对比(五):GetX 的响应式、依赖注册与生命周期
前端·javascript·flutter
广州智造2 小时前
Radioss复合材料使用的单元属性
前端·设计·cad·建模·cae·radioss
程序鉴定师2 小时前
2026年深圳小程序/App开发公司技术选型指南:从架构设计到交付标准全维度评估
java·小程序·uni-app·php·objective-c
张宏宇2 小时前
我把微软开源的 tgrep 做成了本地版 GitHub Code Search:45,629 个文件里搜一次 10ms
前端·后端
huakoh2 小时前
MCP 调用超时后别急着重试:三步读回分清执行状态
前端
打工仔折腾 AI2 小时前
把模型切换交给平台:用蓝耘智能路由搭建商品评论分析工具
java·服务器·前端·后端·python·性能优化·ai agent 实战
不可能片场2 小时前
Electron 发布标题的冒号 击穿命令行解析
前端·electron
500842 小时前
React Native for OpenHarmony 实战:三方库 react-native-crypto-js 的鸿蒙化适配指南
javascript·react native·性能优化·electron·harmonyos