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

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

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

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

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

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

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

只要新建一個tarBar.wxml模板頁,然后引用模板的頁面?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>

接下來進行測試,首先是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}}" />

最后演示如下:

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

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;//根據(jù)頁面地址設置當前頁面狀態(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": "主頁",
     "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數(shù)據(jù)
  var that=this;
 }
})

最終演示和上圖一致!

附:完整demo代碼點擊此處本站下載。

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

相關文章

最新評論

禄丰县| 荆州市| 即墨市| 宣汉县| 凉山| 上栗县| 石林| 华安县| 清原| 成都市| 镇江市| 华蓥市| 乌拉特后旗| 武邑县| 永兴县| 门头沟区| 南漳县| 陕西省| 泗洪县| 湖南省| 株洲县| 白城市| 定襄县| 贡觉县| 三穗县| 喀什市| 类乌齐县| 连南| 大理市| 莱州市| 敦化市| 仙居县| 金阳县| 日照市| 临洮县| 子长县| 卫辉市| 淳化县| 无为县| 华蓥市| 武义县|