這篇文章主要講解了“微信小程序怎么實(shí)現(xiàn)tabBar模板”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“微信小程序怎么實(shí)現(xiàn)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" >
<block wx:for="{{tabBar.list}}" wx:key="pagePath">
<navigator url="{{item.pagePath}}" open-type="redirect" class="menu-item" >
<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 >//相當(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;
}
})感謝各位的閱讀,以上就是“微信小程序怎么實(shí)現(xiàn)tabBar模板”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)微信小程序怎么實(shí)現(xiàn)tabBar模板這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
當(dāng)前標(biāo)題:微信小程序怎么實(shí)現(xiàn)tabBar模板-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://chinadenli.net/article24/dgpcce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、建站公司、網(wǎng)站策劃、靜態(tài)網(wǎng)站、標(biāo)簽優(yōu)化、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容