项目中需要根据用户角色控制TabBar中各Item的显示和隐藏,然而小程序自带TabBar没有提供隐藏某个item的功能,只好自己动手写了一个。

1、wxml文件

<view class="weui-flex tab-bar" style="color: {{color}}; background: {{backgroundColor}}; {{position=='top'? 'top: 0' : 'bottom: 0'}}; {{borderStyle? (position=='top'? 'border-bottom: solid 1px '+borderStyle + ';' : 'border-top: solid 1px '+borderStyle + ';') : ''}}">
<block wx:for="{{list}}" wx:key="pagePath">
<navigator url="{{item.pagePath}}" open-type="switchTab" class="weui-flex__item menu-item {{item.class}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : selectedColor) : ''}}" wx:if="{{item.hidden!==true}}">
<image src="{{item.selectedIconPath}}" wx:if="{{item.active}}" class="img"></image>
<image src="{{item.iconPath}}" wx:if="{{!item.active}}" class="img"></image>
<text class='tabbar_text'>{{item.text}}</text>
</navigator>
</block>
<view class="clear"></view>
</view>

重点:通过每个item的hidden属性控制是否显示

2、js文件

Component({
options: {
multipleSlots: true // 在组件定义时的选项中启用多slot支持
},
/**
* 组件的属性列表
*/
properties: {
color:{
type:String,
value:''
},
selectedColor:{
type:String,
value:'#1aad19'
},
backgroundColor:{
type:String,
value:''
},
position:{
type:String,
value:'bottom'
},
borderStyle:{
type: String,
value:'#ccc'
},
list:{
type:Array,
value:[]
}
}, /**
* 组件的初始数据
*/
data: { }, /**
* 组件的方法列表
*/
methods: { }
})

重点为list属性,定义为一个Array

3、wxss文件

@import "/style/weui.wxss";

.menu-item{
height:100rpx;
text-align: center;
padding-top: 5rpx;
}
.img{
width: 60rpx;
height: 60rpx;
display: block;
margin:auto;
}
.clear{
clear: both;
}
.tab-bar{
position: fixed;
width:100%
}
.tabbar_text{
font-size: 28rpx;
position:relative;
top:-12rpx;
}

4、使用组件:

在app.js中定义各Tab页签,并根据角色控制是否显示:

getTabList:function(){
var tabItemList = [
{
"pagePath": "/pages/asset/list",
"text": "资产台帐",
"iconPath": "/img/list_gray.png",
"selectedIconPath": "/img/list_blue.png",
"active": false
},
{
"pagePath": "/pages/check/index",
"text": "资产盘点",
"iconPath": "/img/list_gray.png",
"selectedIconPath": "/img/list_blue.png",
"active": false
},
{
"pagePath": "/pages/mine/index",
"text": "个人中心",
"iconPath": "/img/list_gray.png",
"selectedIconPath": "/img/list_blue.png",
"active": false
}
];
return tabItemList;
},
initTabBar:function(activeIdx){
var tabItemList=this.getTabList();
//资产台账,资产管理员可见
tabItemList[0].hidden=!this.isADMIN();
if(activeIdx>=0 && activeIdx<tabItemList.length){
tabItemList[activeIdx].active=true;
} return tabItemList;
}

在页面的wxml中插入组件:

<view class="page">
<view class="page__bd">
<block wx:if='{{isSA}}'>
<view class="weui-cells__title"></view>
<view class="weui-cells weui-cells_after-title">
<navigator url="../config/index" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
<view class="weui-cell__bd">系统配置</view>
<view class="weui-cell__ft weui-cell__ft_in-access"></view>
</navigator>
</view>
</block>
<view class="weui-cells__title"></view>
<view class="weui-cells weui-cells_after-title">
<navigator url="changePassword" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
<view class="weui-cell__bd">修改密码</view>
<view class="weui-cell__ft weui-cell__ft_in-access"></view>
</navigator>
<view class="weui-cell weui-cell_access" hover-class="weui-cell_active" catchtap="logout">
<view class="weui-cell__bd">退出登录</view>
<view class="weui-cell__ft weui-cell__ft_in-access"></view>
</view>
</view>
</view>
</view>
<mfmtTabBar list="{{tabItemList}}"></mfmtTabBar>

页面js:

onShow: function () {
//初始化主Tab标签
var tabItemList = app.initTabBar(2);
this.setData({ tabItemList });
}

5、一个小问题

最初,定义组件的navigator时,使用openType="redirect",运行起来后,切换tab时,Tabbar有瞬间飞出去的感觉,用户体验很不好。

改为openType="switchTab",并在app.json中定义Tabbar(否则switchTab不生效):

"tabBar": {
"list": [
{
"pagePath": "pages/asset/list",
"text": "资产台帐"
},
{
"pagePath": "pages/check/index",
"text": "资产盘点"
},
{
"pagePath": "pages/mine/index",
"text": "个人中心"
}
]
}

在app.js的onlaunch方法里调用wx.hideTabBar接口,隐藏原有TabBar。

问题解决,切换时不再有“飞出去”的感觉

微信小程序自定义TabBar的更多相关文章

  1. 微信小程序自定义 tabbar

    一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...

  2. 微信小程序自定义tabbar的实现

    微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...

  3. 微信小程序自定义tabbar的问题

    个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义 ...

  4. 微信小程序 自定义tabbar实例

    在小程序的开发文档中,对tabbar是这样说明的: 如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 t ...

  5. 微信小程序 - 自定义tabbar

    更新: 2019-1-18:自定义tabbar组件已发布 各种奇葩的需求,造就了我们 wxml <view class="nav-tabs"> <view cla ...

  6. 微信小程序 - 自定义tabbar(组件)

    配置项(关于使用组件) index.wxml <!-- tabBar:tabBar配置 activeIndex: 激活页面下标 slots: 多插槽配置(需与页面一致) --> <t ...

  7. 微信小程序——自定义导航栏

    微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...

  8. 微信小程序自定义弹窗wcPop插件|仿微信弹窗样式

    微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的 ...

  9. 微信小程序-自定义底部导航

    代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

随机推荐

  1. 《SQL 基础教程》第八章:SQL 高级处理

    本章分为两个部分: 窗口函数 GROUPING 运算符 它们用于以「窗口」为单位的排序.计算总和等任务. OLAP 函数 OLAP 定义:OLAP 是 OnLIne Analytical Proces ...

  2. hyperledger fabric相关记录

    打开对接监控软件(statsd或者prometheus)开关 每个peer上 CORE_OPERATIONS_LISTENADDRESS=peer0.orgxxxxxxxxx:9443 CORE_ME ...

  3. CSS设计模式

    关于web设计的网站 https://www.smashingmagazine.com/

  4. JQuery的常用选择器 转

    JQuery的常用选择器 刚开始学JQuery写的如有错误欢迎批评指正 JQuery拥有的选择器可以让我们更快更方便找到想要的元素,然后对相应的元素进行操作 简单介绍一下一些常用的选择器: 1.基本选 ...

  5. linux bash array list

    #定义array ptpArray=()while read linedo #将文件读取内容放到array中,注意作为字符串放,否则空格会被分隔成行ptpArray+=("$line&quo ...

  6. Gradient Boosting, Decision Trees and XGBoost with CUDA ——GPU加速5-6倍

    xgboost的可以参考:https://xgboost.readthedocs.io/en/latest/gpu/index.html 整体看加速5-6倍的样子. Gradient Boosting ...

  7. Haxe东游记(上)part1.5:roadmap

    part1.6 = 常用API参考 1.5.7 -> 官方手册目录/总结/中文化 1.8 -> 官方示例/讲解/总结 1.5.6-> haxe整体结构/解析/综述 part2 = 中 ...

  8. 第二天:python的函 数、循环和条件、类

    https://uqer.io/community/share/54c8af17f9f06c276f651a54 第一天学习了Python的基本操作,以及几种主要的容器类型,今天学习python的函数 ...

  9. org.hibernate.ObjectNotFoundException: No row with the given identifier exists

    维护老系统时出现的问题,出现的原因我简述一下: table1与table2是关联表,T1中有T2的主键 "T1_id",当T1中的 "T2_id" 不为null ...

  10. 关于SASS

    SASS:(是一款辅助编写css的工具 安装之后可以通过同时按window键+“R”键 输入“powershell”进入CMD命令页面: 输入“sass -v”可以查看当前的sass版本 输入“cd ...