最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序tabBar模板用法實(shí)例分析【附demo源碼下載】

 更新時(shí)間:2017年11月28日 11:38:32   作者:草燈  
這篇文章主要介紹了微信小程序tabBar模板用法,結(jié)合具體實(shí)例形式分析了tabBar模板的定義、配置、引用等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了微信小程序tabBar模板用法。分享給大家供大家參考,具體如下:

眾所周知,微信小程序的tabBar都是新開(kāi)頁(yè)面的,而微信文檔上又表明了最多只能打開(kāi)5層頁(yè)面。這樣就很容易導(dǎo)致出問(wèn)題啦,假如我的tabBar有5個(gè)呢?下面是微信原話:

一個(gè)應(yīng)用同時(shí)只能打開(kāi)5個(gè)頁(yè)面,當(dāng)已經(jīng)打開(kāi)了5個(gè)頁(yè)面之后,wx.navigateTo不能正常打開(kāi)新頁(yè)面。請(qǐng)避免多層級(jí)的交互方式,或者使用wx.redirectTo

因此這幾天想著根據(jù)微信tabBar數(shù)組來(lái)自定義模板供頁(yè)面調(diào)用。不過(guò)我在list里面每個(gè)對(duì)象都增加了一個(gè)selectedColor和active屬性,方便對(duì)每個(gè)tabBar當(dāng)前頁(yè)做樣式,如果不傳直接使用設(shè)置的selectedColor。因此這串?dāng)?shù)據(jù)只能設(shè)定在各個(gè)頁(yè)面下,不能設(shè)定在公用的app.js配置文件下,稍微有點(diǎn)代碼冗余,下次研究下怎么直接配置到app.js完善下。

只要新建一個(gè)tarBar.wxml模板頁(yè),然后引用模板的頁(yè)面?zhèn)魅霐?shù)據(jù)即可,代碼如下:

<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>

接下來(lái)進(jìn)行測(cè)試,首先是index.js的配置對(duì)象:

//配置tabBar
  tabBar: {
   "color": "#9E9E9E",
   "selectedColor": "#f00",
   "backgroundColor": "#fff",
   "borderStyle": "#ccc",
   "list": [
    {
     "pagePath": "/pages/index/index",
     "text": "主頁(yè)",
     "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}}" />

接下來(lái)是mine.js文件引入配置對(duì)象:

//配置tabBar
  tabBar: {
   "color": "#9E9E9E",
   "selectedColor": "#f00",
   "backgroundColor": "#fff",
   "borderStyle": "#ccc",
   "list": [
    {
     "pagePath": "/pages/index/index",
     "text": "主頁(yè)",
     "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}}" />

最后演示如下:

方案二,我把配置數(shù)據(jù)統(tǒng)一放在app.js文件,通過(guò)點(diǎn)擊跳轉(zhuǎn)頁(yè)面后在把數(shù)據(jù)添加到當(dāng)前頁(yè)面實(shí)例上,具體做法如下:

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;">//相當(dāng)于Page({})里面的this對(duì)象</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;//根據(jù)頁(yè)面地址設(shè)置當(dāng)前頁(yè)面狀態(tài)
   }
  }
  _curPage.setData({
   tabBar: tabBar
  });
 },
 globalData: {
  userInfo: null,
  //配置tabBar
  tabBar: {
   "color": "#9E9E9E",
   "selectedColor": "#f00",
   "backgroundColor": "#fff",
   "borderStyle": "#ccc",
   "list": [
    {
     "pagePath": "/pages/index/index",
     "text": "主頁(yè)",
     "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頁(yè)面使用:

var App=getApp();
Page({
 data:{
  detail: {},
 },
 onLoad:function(options){
  App.editTabBar();//添加tabBar數(shù)據(jù)
  var that=this;
 }
})

最終演示和上圖一致!

附:完整demo代碼點(diǎn)擊此處本站下載

希望本文所述對(duì)大家微信小程序開(kāi)發(fā)有所幫助。

相關(guān)文章

最新評(píng)論

三台县| 离岛区| 义乌市| 新河县| 彭州市| 金山区| 海淀区| 陆河县| 正蓝旗| 女性| 潼南县| 东台市| 金秀| 鄂州市| 安义县| 虞城县| 二手房| 桂平市| 武安市| 吴川市| 诸暨市| 宝丰县| 舟曲县| 新蔡县| 大悟县| 阳西县| 柏乡县| 米易县| 唐河县| 石嘴山市| 阿拉尔市| 肥乡县| 巴楚县| 剑河县| 县级市| 日喀则市| 美姑县| 车险| 年辖:市辖区| 乐平市| 宕昌县|