广告
返回顶部
首页 > 资讯 > 移动开发 >uniapp 开发小程序之实现不同身份展示不同的 tabbar(底部导航栏),附带相关问题解答
  • 549
分享到

uniapp 开发小程序之实现不同身份展示不同的 tabbar(底部导航栏),附带相关问题解答

uni-app小程序 2023-09-13 13:09:49 549人浏览 薄情痞子
摘要

效果展示:   引言 在开发过程中逐渐意识到uniapp原生的tabbar可能不能满足开发要求,通过浏览博客才选择使用uView的Tabbar 底部导航栏来实现,我选择用的是2X版本 安装 我是使用Hbuilder插件的方式引入的组件库,

效果展示:

 

引言

开发过程中逐渐意识到uniapp原生的tabbar可能不能满足开发要求,通过浏览博客才选择使用uView的Tabbar 底部导航栏来实现,我选择用的是2X版本

安装

我是使用Hbuilder插件的方式引入的组件库,安装配置可以看这篇:

下载安装方式配置 | uView 2.0 - 全面兼容nvue的uni-app生态框架 - uni-app UI框架

插件市场:

uView2.0重磅发布,利剑出鞘,一统江湖 - DCloud 插件市场

现在就开始现实需求

  • 首先在pages.JSON中定义文件路径:
"pages": [{"path": "pages/xxx/xxx","style": {"navigationBarTitleText": "","enablePullDownRefresh": false}},{//消息"path": "pages/xxx/xxx"},{"path": "pages/xxx/xxx","style": {"navigationBarTitleText": "xxxx"}},{"path": "pages/xxx/xxx","style": {"navigationBarTitleText": "","enablePullDownRefresh": false}},        {         xxxxxxxxxxxxxxxxxxxxxx          }]
"tabBar": {"height":"1px",// 配置选中颜色"selectedColor": "#22bf8e","color": "#f8f8f8", // 设置为与背景颜色相同"backgroundColor": "transparent", // 设置为透明// list 每一项"list": [{"pagePath": "pages/xxx/xxx"},{"pagePath": "pages/xxx/xxx"},{"pagePath": "pages/xxx/xxx"},{"pagePath": "pages/xxx/xxx"},{"pagePath": "xxxxxxxxxxxxxx"}]}

配置pages.josn中tabBar的原因:是因为要根据uni.switchTab来跳转页面,而uni.switchTab能够跳转是需要在tabBar中定义pagePath的

tabbar底部栏过多怎么办:pages.josn中tabBar最多只能定义五个,如果超出就不能使用uni.switchTab跳转了,需要用uni.navigateTo跳转,不过这两种跳转的效果是不同的,具体需要按照实际情况

  • 然后定义一个tabbar.js
//身份一let tab1 = [{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"},{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"},{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"},{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"}]// 身份二let tab2 = [{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"},{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"},{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"},{"text": "消息","pagePath": "pages/xxx/xxx","iconPath": "../static/xxx/xxx","selectedIconPath": "../static/xxx/xxx"}]export default [tab1,tab2]
  • 然后在app.Vue中登录,根据身份进行判断赋值,localStorage.set()我是封装的uni.setStorageSync方法
import tabBar from '@/utils/tabbar.js'if(xxxx){localStorage.set('tabBar', tabBar[0])}else if(xxx){localStorage.set('tabBar', tabBar[1])}
  • 然后在app.vue中隐藏掉原生tabbar
onLaunch: function() {uni.hideTabBar()console.log('App onLaunch')},onShow: function() {uni.hideTabBar()console.log('App Show')}
  • 然后定义一个tabbar组件

u-tabbar解惑:

一、如需要自定义图标/颜色,在u-tabbar-item标签中使用插槽active-iconinactive-icon来定义图标和颜色

二、可以通过.u-page__item__slot-icon修改图标大小

三、不在pages.josn中定义list,可通过switch判断,用uni.navigateTo跳转

四、其他属性可查看官网:

Tabbar 底部导航栏 | uView 2.0 - 全面兼容nvue的uni-app生态框架 - uni-app UI框架

  • 然后开始使用tabbar组件 ,正常引用、注册、使用就行
import tabbar from "@/components/tabbar.vue"export default {components: {tabbar}}

注意:哪里需要底部栏,就在哪几个页面引入、注册、使用


其中遇到一个机型问题,苹果手机预览,底部还是会出现一段白底,如何解决?

其实也很简单,在每个使用了tabbar组件的页面中也单独定义隐藏原生tabbar的方法,如下代码

onLoad() {uni.hideTabBar()}

来源地址:https://blog.csdn.net/weixin_52479803/article/details/131452705

--结束END--

本文标题: uniapp 开发小程序之实现不同身份展示不同的 tabbar(底部导航栏),附带相关问题解答

本文链接: https://www.lsjlt.com/news/405979.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作