项目中需要根据用户角色控制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. English trip V2 - 3. A Healthy Diet Teacher:Corrine Key:各种前缀 im- un- in- re- over- under-

    In this lesson you will learn to talk about foot and drink for a healthy diet. 课上内容(Lesson) What do ...

  2. java中的JSON数据转换方法fastjson

    1 maven工程引入fastjson <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  3. Python生产者producer和consumer消费者案例写法,含有多线程,包含队列queue、JoinableQueue队列的用法

    import timeimport random import queuefrom multiprocessing import Process,Queue 案例一:def consumer(q,na ...

  4. 【转载】Jmeter 性能测试入门

    [转载]Jmeter性能测试 入门 Jmeter是一款优秀的开源测试工具, 是每个资深测试工程师,必须掌握的测试工具,熟练使用Jmeter能大大提高工作效率. 熟练使用Jmeter后, 能用Jmete ...

  5. BP neural network optimized by PSO algorithm on Ammunition storage reliability prediction 阅读笔记

    1.BP neural network optimized by PSO algorithm on Ammunition storage reliability prediction 文献简介文献来源 ...

  6. 蓝桥杯第九届省赛 sscanf(),str.c_str()函数的使用

    标题:航班时间 [问题背景]小h前往美国参加了蓝桥杯国际赛.小h的女朋友发现小h上午十点出发,上午十二点到达美国,于是感叹到“现在飞机飞得真快,两小时就能到美国了”. 小h对超音速飞行感到十分恐惧.仔 ...

  7. kubeadm安装kubernetes-v1.13.1

    kubeadm安装kubernetes-v1.13.1 centos虚拟机使用kubeadm安装k8s-v1.13.1. 机器信息如下: 主机名 ip master 192.168.239.200 n ...

  8. 两年前详细分析了ijkplayer的代码

    两年前详细分析了ijkplayer的代码,太久没总结了,现在只能上传一张图片了.把fly替换成ijk就行了. 用diagramDesigner画的,因为这个工具足够简单,用visio不知道要画到什么时 ...

  9. 通讯录管理系统(C语言)

    /* * 对通讯录进行插入.删除.排序.查找.单个显示功能 */ #include <stdio.h> #include <malloc.h> #include <str ...

  10. 第九次java课堂笔记