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>
相关推荐
阿珊和她的猫4 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
加班是不可能的,除非双倍日工资8 小时前
css预编译器实现星空背景图
前端·css·vue3
wyiyiyi9 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
gnip9 小时前
vite和webpack打包结构控制
前端·javascript
excel9 小时前
在二维 Canvas 中模拟三角形绕 X、Y 轴旋转
前端
阿华的代码王国10 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
一条上岸小咸鱼10 小时前
Kotlin 基本数据类型(三):Booleans、Characters
android·前端·kotlin
Jimmy10 小时前
AI 代理是什么,其有助于我们实现更智能编程
前端·后端·ai编程
ZXT10 小时前
promise & async await总结
前端
Jerry说前后端10 小时前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化