這篇文章主要講解了“微信小程序怎么實(shí)現(xiàn)tabBar模板”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“微信小程序怎么實(shí)現(xiàn)tabBar模板”吧!
景寧畬族自治網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)2013年至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
眾所周知,微信小程序的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配置文件下,稍微有點(diǎn)代碼冗余,下次研究下怎么直接配置到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>
接下來進(jìn)行測試,首先是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文件,通過點(diǎn)擊跳轉(zhuǎn)頁面后在把數(shù)據(jù)添加到當(dāng)前頁面實(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對象</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; } })
感謝各位的閱讀,以上就是“微信小程序怎么實(shí)現(xiàn)tabBar模板”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對微信小程序怎么實(shí)現(xiàn)tabBar模板這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)頁題目:微信小程序怎么實(shí)現(xiàn)tabBar模板
網(wǎng)站網(wǎng)址:http://chinadenli.net/article10/gopgdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站排名、商城網(wǎng)站、動態(tài)網(wǎng)站、虛擬主機(jī)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)