微信小程式編寫tabBar模板,map元件markers屬性動態初始化
阿新 • • 發佈:2019-01-22
眾所周知,微信小程式的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>
-
//配置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"
- }
-
<import src="../../template/tabBar.wxml" />
- <template is="tabBar" data="{{tabBar: tabBar}}" />
-
//配置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"
- }
-
<import src="../../template/tabBar.wxml" />
- <template is="tabBar" data="{{tabBar: tabBar}}" />
方案二
我把配置資料統一放在app.js檔案,通過點選跳轉頁面後在把資料新增到當前頁面例項上,具體做法如下:
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.active=false;
-
if(tabBar.list.pagePath==_pagePath){
-
tabBar.list.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"
-
}
-
}
- })
-
var App=getApp();
-
Page({
-
data:{
-
detail: {},
-
},
-
onLoad:function(options){
-
App.editTabBar();//新增tabBar資料
-
var that=this;
-
}
- })
二:map元件markers屬性動態初始化
今天在寫小程式詳情頁時候遇到一個問題,map元件的markers屬性是通過非同步請求到資料後設置的,結果就導致了微信底層渲染出錯。但是如果我先在data初始化markers變數,每次小程式都不能正常渲染都是初始化的北京的資料。然後寫了如下測試:
test.js:
-
Page({
-
data:{
-
map:{
-
lat: 0,
-
lng: 0,
-
markers: [],
-
hasMarkers: false//解決方案
-
}
-
},
-
onLoad: function(options){
-
var that=this;
-
wx.getLocation({
-
type: 'wgs84', // 預設為 wgs84 返回 gps 座標,gcj02 返回可用於 wx.openLocation 的座標
-
success: function (res) {
-
// success
-
wx.request({
-
url: 'https://xxx.com/detail.htm?vid=3&latlng=' + res.latitude + ',' + res.longitude,
-
data: {},
-
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
-
// header: {}, // 設定請求的 header
-
success: function (res) {
-
// success
-
that.setData({
-
'map.lat': res.data.data.lat,
-
'map.lng': res.data.data.lng,
-
'map.markers': [{
-
latitude: res.data.data.lat,
-
longitude: res.data.data.lng,
-
name: res.data.data.title,
-
desc: res.data.data.address
-
}],
-
'map.hasMarkers': true//解決方案
-
});
-
console.log(that.data.map.markers);
-
}
-
})
-
}
-
})
-
}
- });
-
<view style="height: 100rpx;">
-
{{map.lat}}--{{map.lng}}--{{map.markers[0].name}}--{{map.markers[0].desc}}
-
</view>
- <map latitude="{{map.lat}}" longitude="{{map.lng}}" markers="{{map.markers}}"></map>
測試二:初始化map變數,並在wxml把map資料都打印出來
為什麼會這樣呢??各種測試,最後發現只要不是經過遠端請求在來設定map值就不會出問題。
後來經大神網友提點,由於非同步設定值的過程,元件已經渲染。但是map變數沒有預定義是undefined狀態,map初始化拿不到資料直接報錯了,而非同步過來又變成動態更新導致了wxml需要重新渲染map元件的情況導致的。因為官方文件有提到:
地圖元件的經緯度必填, 如果不填經緯度則預設值是北京的經緯度。 標記點markers只能在初始化的時候設定,不支援動態更新。
只能初始化一次,因此導致每次都是顯示的初始化資訊。
然後,網友給出的解決方案完美解決了這個問題:
wx:if會渲染一下元件,那我們按照這個思路給他加個if就行了
- <map markers="{{markers}}" style="width: 375px; height: 200px;" wx:if="{{map}}"></map>
預設map是false,就是載入時不渲染map元件,等ajax回來後把map設定為true,這樣就動態渲染成你要的地址了
原文連結:http://bbs.jointforce.com/topic/25439