uni-app自定义底部tab并且根据字段显示和隐藏

news/2024/11/27 9:00:02/

首先将所有tab使用到的页面创建好并且在pages里面配置好,要在pages.json中的"tabBar里面配置"custom": true将自带的tab底部导航关闭

"pages": [{"path": "pages/mine/mine","style": {"navigationBarTitleText": "我的"}},{"path": "pages/home/home","style": {"navigationBarTitleText": "首页"}},{"path": "pages/Operation/Operation","style": {"navigationBarTitleText": "运维"}},{"path": "pages/index/index","style": {"navigationBarTitleText": "服务"}},{"path" : "pages/shebe/shebe","style" : {"navigationBarTitleText" : "设备"}}],
"tabBar": {"custom": true,"color": "#7A7E83","selectedColor": "#1296db","borderStyle": "black","backgroundColor": "#F8F8F8","list": [{"pagePath": "pages/home/home","text": "首页","iconPath": "static/imgs/imgs55.png","selectedIconPath": "static/imgs/imgs5.png"},{"pagePath": "pages/Operation/Operation","text": "运维","iconPath": "static/imgs/imgs66.png","selectedIconPath": "static/imgs/imgs6.png"},{"pagePath": "pages/shebe/shebe","text": "设备","iconPath": "static/imgs/imgs66.png","selectedIconPath": "static/imgs/imgs6.png"},{"pagePath": "pages/index/index","text": "服务","iconPath": "static/imgs/imgs11.png","selectedIconPath": "static/imgs/imgs1.png"},{"pagePath": "pages/mine/mine","text": "我的","iconPath": "static/imgs/imgs44.png","selectedIconPath": "static/imgs/imgs4.png"}]},

uni-app官网找到官网插件搜索tab底部找到自己要用的插件如(custom-tab-bar 自定义底部导航栏 - DCloud 插件市场)

在每一个用到tab页面的底部使用

 <CustomTabBar modifyType="index"></CustomTabBar>
import CustomTabBar from "@/components/custom-tab-bar/index.vue";
export default {components: {CustomTabBar}},
}

中modifyType="index"中的index根据自己定义的路径来定,在那个页面用感觉那个页面来定

如我在mine页面用代码就是

  <CustomTabBar modifyType="mine"></CustomTabBar>
import CustomTabBar from "@/components/custom-tab-bar/index.vue";

如何根据权限设置tab显示几个,要找到我们引入的tab页面,然后根据相关要求进行更改

<template><view v-if="responseRoles.includes('sitemanage')"><view class="tab-main"><view class="tabs" :class="{ spaceCenter: tabList.length === 1 }"><view class="tab-item" :class="{ active: currentIndex === index }" v-for="(item, index) in tabList":key="index" @click.stop="handleTab(item, index)"><image class="img" :src="currentIndex === index ? item.selectedIconPath : item.iconPath"></image><view class="text">{{ item.label }}</view></view></view></view></view><view v-if="responseRoles.includes('electricCollection')"><view class="tab-main"><view class="tabs" :class="{ spaceCenter: tabList.length === 1 }"><view class="tab-item" :class="{ active: currentIndex === index }" v-for="(item, index) in tabsTab":key="index" @click.stop="handleTab(item, index)"><image class="img" :src="currentIndex === index ? item.selectedIconPath : item.iconPath"></image><view class="text">{{ item.label }}</view></view></view></view></view><view v-if="responseRoles.includes('operation')"><view class="tab-main"><view class="tabs" :class="{ spaceCenter: tabList.length === 1 }"><view class="tab-item" :class="{ active: currentIndex === index }" v-for="(item, index) in tabList":key="index" @click.stop="handleTab(item, index)"><image class="img" :src="currentIndex === index ? item.selectedIconPath : item.iconPath"></image><view class="text">{{ item.label }}</view></view></view></view></view></template><script>import {appgetInfo} from '@/src/api/api.js';export default {props: {modifyType: {type: String,default: "",},},computed: {tabList() {return this.tabs.filter((item) => item.show);},},data() {return {currentIndex: 0,responseRoles: [],tabs: [{label: "首页",type: "home",url: "/pages/home/home",iconPath: "/static/imgs/imgs55.png",selectedIconPath: "/static/imgs/imgs5.png",show: true,},{label: "运维",type: "Operation",url: "/pages/Operation/Operation",iconPath: "/static/imgs/imgs66.png",selectedIconPath: "/static/imgs/imgs6.png",show: true,},{label: "服务",type: "index",url: "/pages/index/index",iconPath: "/static/imgs/imgs11.png",selectedIconPath: "/static/imgs/imgs1.png",show: true,},{label: "我的",type: "mine",url: "/pages/mine/mine",iconPath: "/static/imgs/imgs44.png",selectedIconPath: "/static/imgs/imgs4.png",show: true,}],tabsTab: [{label: "首页",type: "home",url: "/pages/home/home",iconPath: "/static/imgs/imgs55.png",selectedIconPath: "/static/imgs/imgs5.png",show: true,},{label: "设备",type: "shebe",url: "/pages/shebe/shebe",iconPath: "/static/imgs/imgs66.png",selectedIconPath: "/static/imgs/imgs6.png",show: true,},{label: "服务",type: "index",url: "/pages/index/index",iconPath: "/static/imgs/imgs11.png",selectedIconPath: "/static/imgs/imgs1.png",show: true,},{label: "我的",type: "mine",url: "/pages/mine/mine",iconPath: "/static/imgs/imgs44.png",selectedIconPath: "/static/imgs/imgs4.png",show: true,}]};},methods: {handleTab(item, index) {if (this.currentIndex === index) {return;}uni.switchTab({url: item.url,});},async fetchTabData() {try {const response = await appgetInfo();this.responseRoles = response.roles;} catch (error) {console.error("获取数据失败:", error);}},},mounted() {this.fetchTabData();if (this.modifyType.length) {this.currentIndex = this.tabList.findIndex((item) => item.type === this.modifyType);if(this.currentIndex===-1){this.currentIndex=1};}},};
</script><style lang="scss" scoped>.tab-main {position: fixed;z-index: 9999;width: 100%;bottom: 0;left: 0;background: #ffffff;box-shadow: 0rpx -1rpx 0rpx 0rpx #ebedf0;.tabs {display: flex;justify-content: space-between;padding: 0 50rpx;height: 120rpx;.tab-item {padding: 16rpx 50rpx;display: flex;align-items: center;flex-direction: column;.img {width: 50rpx;height: 50rpx;}.text {margin-top: 12rpx;font-size: 24rpx;font-weight: 500;color: #5d5d5d;line-height: 24rpx;}}.tab-item.active {.text {color: #4199d1;}}}.spaceCenter {justify-content: center;}}
</style>


http://www.ppmy.cn/news/1550299.html

相关文章

macOS上进行Ant Design Pro实战教程(一)

由于一个AI项目的前端使用了umi&#xff0c;本教程根据阿里官网上的 《Ant Design 实战教程&#xff08;beta 版&#xff09;》来实操一下&#xff0c;我使用macOS操作系统&#xff0c;VS Code 开发环境。 一、开发环境 1、安装nodejs, npm, yarn 官网上建议使用cnpm&#xf…

MySQL查询缓存详解

一、查询缓存的基本概念 MySQL 的查询缓存是一种用于存储查询结果的内存区域。当一个查询被执行时&#xff0c;MySQL 首先检查查询缓存中是否已经存在相同的查询结果。如果存在&#xff0c;直接从查询缓存中返回结果&#xff0c;而无需再次执行查询语句&#xff0c;从而大大提高…

蓝桥杯c++算法秒杀【6】之动态规划【下】(数字三角形、砝码称重(背包问题)、括号序列、异或三角:::非常典型的必刷例题!!!)

别忘了请点个赞收藏关注支持一下博主喵&#xff01;&#xff01;&#xff01;! ! ! ! &#xff01; 关注博主&#xff0c;更多蓝桥杯nice题目静待更新:) 动态规划 三、括号序列 【问题描述】 给定一个括号序列&#xff0c;要求尽可能少地添加若干括号使得括号序列变得合…

使用mingw+CMake在Windows平台编译OpenCV

1. 安装mingw和cmake cmake的安装比较简单&#xff0c;百度一下完成相关操作即可&#xff0c;笔者安装的是3.24.3版本。 Mingw的安装也有很多相关文章&#xff0c;不过我使用的是安装QT时附带安装的mingw&#xff0c;其路径为D:\software\Qt\Tools\mingw1120_64。其中的bin文件…

【数据结构实战篇】用C语言实现你的私有队列

&#x1f3dd;️专栏&#xff1a;【数据结构实战篇】 &#x1f305;主页&#xff1a;f狐o狸x 在前面的文章中我们用C语言实现了栈的数据结构&#xff0c;本期内容我们将实现队列的数据结构 一、队列的概念 队列&#xff1a;只允许在一端进行插入数据操作&#xff0c;在另一端…

租赁小程序|租赁系统搭建|租赁系统需求

随着信息技术的高速发展&#xff0c;租赁行业逐渐向智能化、便捷化方向迈进。一款优秀的租赁小程序&#xff0c;旨在为用户提供一站式的租赁服务体验&#xff0c;同时帮助租赁企业优化管理流程&#xff0c;提高业务效率。 一、用户需求精准把握 在开发任何软件产品时&#xff0…

C# 结构体

文章目录 前言一、结构体的定义与基本使用&#xff08;一&#xff09;定义结构体&#xff08;二&#xff09;结构体的使用示例 二、C# 结构的特点&#xff08;一&#xff09;丰富的成员类型&#xff08;二&#xff09;构造函数相关限制与特性&#xff08;三&#xff09;继承方面…

JavaScript的扩展运算符...

在JavaScript的世界里&#xff0c;扩展运算符&#xff08;...&#xff09;是一个多功能的工具&#xff0c;它可以在多种不同的上下文中使用&#xff0c;以实现不同的功能。从数组到对象&#xff0c;再到函数&#xff0c;扩展运算符都能大显身手。本文将深入探讨扩展运算符的用法…