当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序tabBar模板用法实例分析【附demo源码下载】

微信小程序tabBar模板用法实例分析【附demo源码下载】

2017年12月08日  | 移动技术网IT编程  | 我要评论

本文实例讲述了微信小程序tabbar模板用法。分享给大家供大家参考,具体如下:

众所周知,微信小程序的tabbar都是新开页面的,而微信文档上又表明了最多只能打开5层页面。这样就很容易导致出问题啦,假如我的tabbar有5个呢?下面是微信原话:

一个应用同时只能打开5个页面,当已经打开了5个页面之后,wx.navigateto不能正常打开新页面。请避免多层级的交互方式,或者使用wx.redirectto

因此这几天想着根据微信tabbar数组来自定义模板供页面调用。不过我在list里面每个对象都增加了一个selectedcolor和active属性,方便对每个tabbar当前页做样式,如果不传直接使用设置的selectedcolor。因此这串数据只能设定在各个页面下,不能设定在公用的app.js配置文件下,稍微有点代码冗余,下次研究下怎么直接配置到app.js完善下。

只要新建一个tarbar.wxml模板页,然后引用模板的页面传入数据即可,代码如下:

<template name="tabbar">
 <view class="flex-h flex-hsb tab-bar" style="color: {{tabbar.color}}; background: {{tarbar.backgroundcolor}}; {{tabbar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabbar.borderstyle? (tabbar.position=='top'? 'border-bottom: solid 1px '+tabbar.borderstyle + ';' : 'border-top: solid 1px '+tabbar.borderstyle + ';') : ''}}">
 <block wx:for="{{tabbar.list}}" wx:key="pagepath">
  <navigator url="{{item.pagepath}}" open-type="redirect" class="menu-item" style="{{item.active? 'color: '+(item.selectedcolor? item.selectedcolor : tabbar.selectedcolor) : ''}}">
   <image src="{{item.selectediconpath}}" wx:if="{{item.active}}"></image>
   <image src="{{item.iconpath}}" wx:if="{{!item.active}}"></image>
   <text>{{item.text}}</text>
  </navigator>
  </block>
 </view>
</template>

接下来进行测试,首先是index.js的配置对象:

//配置tabbar
  tabbar: {
   "color": "#9e9e9e",
   "selectedcolor": "#f00",
   "backgroundcolor": "#fff",
   "borderstyle": "#ccc",
   "list": [
    {
     "pagepath": "/pages/index/index",
     "text": "主页",
     "iconpath": "../../img/tabbar_home.png",
     "selectediconpath": "../../img/tabbar_home_cur.png",
     //"selectedcolor": "#4edf80",
     active: true
    },
    {
     "pagepath": "/pages/village/city/city",
     "text": "目的地",
     "iconpath": "../../img/tabbar_village.png",
     "selectediconpath": "../../img/tabbar_village_cur.png",
     "selectedcolor": "#4edf80",
     active: false
    },
    {
     "pagepath": "/pages/mine/mine",
     "text": "我的",
     "iconpath": "../../img/tabbar_mine.png",
     "selectediconpath": "../../img/tabbar_mine_cur.png",
     "selectedcolor": "#4edf80",
     active: false
    }
   ],
   "position": "bottom"
  }

index.wxml引入模板:

<import src="../../template/tabbar.wxml" />
<template is="tabbar" data="{{tabbar: tabbar}}" />

接下来是mine.js文件引入配置对象:

//配置tabbar
  tabbar: {
   "color": "#9e9e9e",
   "selectedcolor": "#f00",
   "backgroundcolor": "#fff",
   "borderstyle": "#ccc",
   "list": [
    {
     "pagepath": "/pages/index/index",
     "text": "主页",
     "iconpath": "../../img/tabbar_home.png",
     "selectediconpath": "../../img/tabbar_home_cur.png",
     //"selectedcolor": "#4edf80",
     active: false
    },
    {
     "pagepath": "/pages/village/city/city",
     "text": "目的地",
     "iconpath": "../../../img/tabbar_village.png",
     "selectediconpath": "../../../img/tabbar_village_cur.png",
     "selectedcolor": "#4edf80",
     active: false
    },
    {
     "pagepath": "/pages/mine/mine",
     "text": "我的",
     "iconpath": "../../img/tabbar_mine.png",
     "selectediconpath": "../../img/tabbar_mine_cur.png",
     "selectedcolor": "#4edf80",
     active: true
    }
   ],
   "position": "bottom"
  }

mine.wxml引入模板:

<import src="../../template/tabbar.wxml" />
<template is="tabbar" data="{{tabbar: tabbar}}" />

最后演示如下:

方案二,我把配置数据统一放在app.js文件,通过点击跳转页面后在把数据添加到当前页面实例上,具体做法如下:

1、app.js文件配置:

//app.js
var net = require('common/net');
var a_l, a_d = {}, a_cbsucc, a_cbsuccfail, a_cbfail, a_cbcom, a_h, a_m;
app({
 onlaunch: function () {
  var that = this;
 },
 //修改tabbar的active值
 edittabbar: function () {
  var _curpagearr = getcurrentpages();
  var _curpage = _curpagearr[_curpagearr.length - 1];<span style="font-family: arial, helvetica, sans-serif;">//相当于page({})里面的this对象</span>
  var _pagepath=_curpage.__route__;
  if(_pagepath.indexof('/') != 0){
   _pagepath='/'+_pagepath;
  }
  var tabbar=this.globaldata.tabbar;
  for(var i=0; i<tabbar.list.length; i++){
   tabbar.list[i].active=false;
   if(tabbar.list[i].pagepath==_pagepath){
    tabbar.list[i].active=true;//根据页面地址设置当前页面状态
   }
  }
  _curpage.setdata({
   tabbar: tabbar
  });
 },
 globaldata: {
  userinfo: null,
  //配置tabbar
  tabbar: {
   "color": "#9e9e9e",
   "selectedcolor": "#f00",
   "backgroundcolor": "#fff",
   "borderstyle": "#ccc",
   "list": [
    {
     "pagepath": "/pages/index/index",
     "text": "主页",
     "iconpath": "/pages/templateimg/tabbar_home.png",
     "selectediconpath": "/pages/templateimg/tabbar_home_cur.png",
     "selectedcolor": "#4edf80",
     active: false
    },
    {
     "pagepath": "/pages/village/city/city",
     "text": "目的地",
     "iconpath": "/pages/templateimg/tabbar_village.png",
     "selectediconpath": "/pages/templateimg/tabbar_village_cur.png",
     "selectedcolor": "#4edf80",
     active: false
    },
    {
     "pagepath": "/pages/mine/mine",
     "text": "我的",
     "iconpath": "/pages/templateimg/tabbar_mine.png",
     "selectediconpath": "/pages/templateimg/tabbar_mine_cur.png",
     "selectedcolor": "#4edf80",
     active: false
    }
   ],
   "position": "bottom"
  }
 }
})

2、index.js+mine.js+city.js页面使用:

var app=getapp();
page({
 data:{
  detail: {},
 },
 onload:function(options){
  app.edittabbar();//添加tabbar数据
  var that=this;
 }
})

最终演示和上图一致!

附:完整demo代码点击此处。

希望本文所述对大家微信小程序开发有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网