项目中需要根据用户角色控制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. 雷林鹏分享:jQuery EasyUI 数据网格 - 动态改变列

    jQuery EasyUI 数据网格 - 动态改变列 数据网格(DataGrid)列可以使用 'columns' 属性简单地定义.如果您想动态地改变列,那根本没有问题.为了改变列,您可以重新调用dat ...

  2. NetSec2019 20165327 Exp1 PC平台逆向破解

    NetSec2019 20165327 Exp1 PC平台逆向破解 一.实践目标 本次实践的对象是一个名为pwn1(实验中用的 是5327pwnx)的linux可执行文件. 该程序正常执行流程是:ma ...

  3. gm图片处理器:The gm/convert binaries can't be found

    今天在使用node处理模块gm的时候,总是无法正常使用,报:The gm/convert binaries can't be found(找不到gm/convert二进制文件) google了一下,发 ...

  4. WARING

    每一道题都先手玩样例! 认真读一下每一档数据,仔细计算每一档可以拿的分数! 读完题目后,把所有能想到的思路写在纸上. 最优化题目考虑dp和贪心两种方法 字符串题目前缀考虑trie树,后缀考虑fail树 ...

  5. windows 安装xadmin

    1.访问github :https://github.com/sshwsfc/xadmin 2.新建README.rst 并替换到下载的zip文件中 3.cmd下,进入虚拟环境使用pip instal ...

  6. 同步请求和异步请求的区别,ajax异步请求如何理解

    同步请求和异步请求的区别 先解释一下同步和异步的概念 同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 异步是指:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的 ...

  7. 使用shiro的密码服务模块

    http://jinnianshilongnian.iteye.com/blog/2021439 http://www.cnblogs.com/snidget/p/3817763.html

  8. Linux 配置ssh 免密码登录

    在平常应用中,我们经常会登录到其他主机,比如说服务器,每次都需要用户名和密码. 我们可以通过ssh免密码登录服务器而不需要输入密码. 现在有一台ubuntu的阿里云服务器,称之为 server.  公 ...

  9. 微信小程序 wx.request

    onLoad: function () { var that = this console.log('https://free-api.heweather.com/s6/weather?locatio ...

  10. Jan.07

    Jan.07 英文朗读 be good for对...好 be bad for/be harmful to对..不利. Smoking is not good for your health. 吸烟对 ...