效果展示:
引言
在开发过程中逐渐意识到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.switchTab跳转,不过这两种跳转的效果是不同的,具体需要按照实际情况
- 然后定义一个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组件
<template><view><u-tabbar class="tabbar-ios-fix" activeColor='#22bf8e' :value="current?current:0" :fixed="true":placeholder="true" :safeAreaInsetBottom="true" @change="handleTabClick"><u-tabbar-item v-for='(item,index) in tabList' :key="index" :text="item.text"><image slot="inactive-icon" class="u-page__item__slot-icon" :src="item.iconPath"></image><image slot="active-icon" class="u-page__item__slot-icon" :src="item.selectedIconPath"></image></u-tabbar-item></u-tabbar></view>
</template><script>import localStorage from '@/utils/localStorage.js'export default {props: {current: Number},data() {return {tabList: localStorage.get('tabBar')};},mounted() {console.log(this.tabList)},methods: {handleTabClick(index) {console.log(index)if (index >= 3) {console.log('切换到我的')switch (index) {case 3:uni.navigateTo({url: '/pages/mine/mine',});break;}return}// 使用 uni.switchTab 方法切换页面uni.switchTab({url: '/' + this.tabList[index].pagePath,});},}}
</script><style lang="scss">.u-page__item__slot-icon {width: 50rpx;height: 50rpx;}.tabbar-ios-fix {bottom: calc(120rpx + env(safe-area-inset-bottom));}
</style>
u-tabbar解惑:
一、如需要自定义图标/颜色,在u-tabbar-item
标签中使用插槽active-icon
和inactive-icon
来定义图标和颜色
二、可以通过.u-page__item__slot-icon修改图标大小
三、不在pages.josn中定义list,可通过switch判断,用uni.navigateTo跳转
四、其他属性可查看官网:
Tabbar 底部导航栏 | uView 2.0 - 全面兼容nvue的uni-app生态框架 - uni-app UI框架
- 然后开始使用tabbar组件 ,正常引用、注册、使用就行
<tabbar :current="0"></tabbar>import tabbar from "@/components/tabbar.vue"
export default {components: {tabbar}
}
注意:哪里需要底部栏,就在哪几个页面引入、注册、使用
其中遇到一个机型问题,苹果手机预览,底部还是会出现一段白底,如何解决?
其实也很简单,在每个使用了tabbar组件的页面中也单独定义隐藏原生tabbar的方法,如下代码
onLoad() {uni.hideTabBar()
}