项目中需要根据用户角色控制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. 用Python实现支持向量机并处理Iris数据集

    SVM全称是Support Vector Machine,即支持向量机,是一种监督式学习算法.它主要应用于分类问题,通过改进代码也可以用作回归.所谓支持向量就是距离分隔面最近的向量.支持向量机就是要确 ...

  2. 解密:Amazon亚马逊产品Listing关键词刷单排名原理

    第一:基本概念   在阅读正文之前,请先理解下面的几个基本概念.   核心词[高频词.热词.大词.主词]:长度大约1~2个单词构成,指每天搜索量比较多的词,每个行业对高频词的划分不一样,这个主要看自己 ...

  3. LeetCode--029--两数相除(java)

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 1: 输 ...

  4. SpringBoot使用日志

    1.日志框架 日志门面 日志实现 JCL.SLF4J.jboss-logging Log4j.JUL.Log4j2.Logback 日志门面:SLF4J 日志实现:Logback SpringBoot ...

  5. windows 下 修改jmeter ServerAgent端口

    from:https://blog.csdn.net/wanglha/article/details/51281462 如果想修改UDP和TCP的端口该如何做呢,可以采用如下的方式: CMD命令进入S ...

  6. .net mvc 上传头像

    我用的是mvc5  开发环境vs2017 [仅供参考] [视图代码] <div > <img src="@path" alt="@att.Count&q ...

  7. vmware12启动centos6.8报错ACPI:memory_hp:Memory online failed

    报错信息 打开后出现黑屏上只显示 ACPI:memory_hp:Memory online failed for 0x10000000 - 0x80000000 BUG: soft lockup - ...

  8. CentOS7系统系统检查脚本

    #!/bin/bash ############################################################## # File Name: .sh # Versio ...

  9. CentOS7系统上的GPSTK示例代码调试 & 运行结果 & 心得

    下载的源码程序包中,共有16个例子,这里记录它们的调试及运行结果,尤其是哪些可用,哪些不可用,今后使用时用作参考. 总结: (1)在 18 个示例程序中,example16 和 example17 编 ...

  10. vim 多标签和多窗口

    多文件编辑时的利器啊.谁用谁知道. 多标签 一次性以标签的形式打开多个文件: vim -p *.cpp :tabnew 增加一个标签 :tabc       关闭当前的tab :tabo       ...