您好,登錄后才能下訂單哦!
這篇文章主要講解了“微信小程序怎么實現(xiàn)tabBar模板”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“微信小程序怎么實現(xiàn)tabBar模板”吧!
眾所周知,微信小程序的tabBar都是新開頁面的,而微信文檔上又表明了最多只能打開5層頁面。這樣就很容易導(dǎo)致出問題啦,假如我的tabBar有5個呢?下面是微信原話:
一個應(yīng)用同時只能打開5個頁面,當(dāng)已經(jīng)打開了5個頁面之后,wx.navigateTo
不能正常打開新頁面。請避免多層級的交互方式,或者使用wx.redirectTo
因此這幾天想著根據(jù)微信tabBar數(shù)組來自定義模板供頁面調(diào)用。不過我在list里面每個對象都增加了一個selectedColor和active屬性,方便對每個tabBar當(dāng)前頁做樣式,如果不傳直接使用設(shè)置的selectedColor。因此這串?dāng)?shù)據(jù)只能設(shè)定在各個頁面下,不能設(shè)定在公用的app.js配置文件下,稍微有點代碼冗余,下次研究下怎么直接配置到app.js完善下。
只要新建一個tarBar.wxml模板頁,然后引用模板的頁面?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>
接下來進行測試,首先是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文件,通過點擊跳轉(zhuǎn)頁面后在把數(shù)據(jù)添加到當(dāng)前頁面實例上,具體做法如下:
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對象</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ù)頁面地址設(shè)置當(dāng)前頁面狀態(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; } })
感謝各位的閱讀,以上就是“微信小程序怎么實現(xiàn)tabBar模板”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對微信小程序怎么實現(xiàn)tabBar模板這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。