微信小程式tabBar模板用法執行個體分析【附demo源碼下載】,tabbardemo

來源:互聯網
上載者:User

小程式tabBar模板用法執行個體分析【附demo源碼下載】,tabbardemo

本文執行個體講述了小程式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.jsvar 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代碼點擊此處本站下載

希望本文所述對大家小程式開發有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.