微信小程序编写tabBar模板,map组件markers属性动态初始化

本文主要是介绍微信小程序编写tabBar模板,map组件markers属性动态初始化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一:编写tabBar模板

众所周知,微信小程序的tabBar都是新开页面的,而微信文档上又表明了最多只能打开5层页面。这样就很容易导致出问题啦,假如我的tabBar有5个呢?下面是微信原话:

一个应用同时只能打开5个页面,当已经打开了5个页面之后,wx.navigateTo不能正常打开新页面。请避免多层级的交互方式,或者使用wx.redirectTo

因此这几天想着根据微信tabBar数组来自定义模板供页面调用。不过我在list里面每个对象都增加了一个selectedColor和active属性,方便对每个tabBar当前页做样式,如果不传直接使用设置的selectedColor。因此这串数据只能设定在各个页面下,不能设定在公用的app.js配置文件下,稍微有点代码冗余,下次研究下怎么直接配置到app.js完善下。

只要新建一个tarBar.wxml模板页,然后引用模板的页面传入数据即可,代码如下:
  1. <template name="tabBar">  
  2.   <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 + ';') : ''}}">  
  3.   <block wx:for="{{tabBar.list}}" wx:key="pagePath">  
  4.     <navigator url="{{item.pagePath}}" open-type="redirect" class="menu-item" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}">  
  5.       <image src="{{item.selectedIconPath}}" wx:if="{{item.active}}"></image>  
  6.       <image src="{{item.iconPath}}" wx:if="{{!item.active}}"></image>  
  7.       <text>{{item.text}}</text>  
  8.     </navigator>  
  9.     </block>  
  10.   </view>  
  11. </template>  
复制代码
接下来进行测试,首先是index.js的配置对象:
  1. //配置tabBar  
  2.     tabBar: {  
  3.       "color": "#9E9E9E",  
  4.       "selectedColor": "#f00",  
  5.       "backgroundColor": "#fff",  
  6.       "borderStyle": "#ccc",  
  7.       "list": [  
  8.         {  
  9.           "pagePath": "/pages/index/index",  
  10.           "text": "主页",  
  11.           "iconPath": "../../img/tabBar_home.png",  
  12.           "selectedIconPath": "../../img/tabBar_home_cur.png",  
  13.           //"selectedColor": "#4EDF80",  
  14.           active: true  
  15.         },  
  16.         {  
  17.           "pagePath": "/pages/village/city/city",  
  18.           "text": "目的地",  
  19.           "iconPath": "../../img/tabBar_village.png",  
  20.           "selectedIconPath": "../../img/tabBar_village_cur.png",  
  21.           "selectedColor": "#4EDF80",  
  22.           active: false  
  23.         },  
  24.         {  
  25.           "pagePath": "/pages/mine/mine",  
  26.           "text": "我的",  
  27.           "iconPath": "../../img/tabBar_mine.png",  
  28.           "selectedIconPath": "../../img/tabBar_mine_cur.png",  
  29.           "selectedColor": "#4EDF80",  
  30.           active: false  
  31.         }  
  32.       ],  
  33.       "position": "bottom"  
  34.     }
复制代码
index.wxml引入模板:
  1. <import src="../../template/tabBar.wxml" />  
  2. <template is="tabBar" data="{{tabBar: tabBar}}" />  
复制代码
接下来是mine.js文件引入配置对象:
  1. //配置tabBar  
  2.     tabBar: {  
  3.       "color": "#9E9E9E",  
  4.       "selectedColor": "#f00",  
  5.       "backgroundColor": "#fff",  
  6.       "borderStyle": "#ccc",  
  7.       "list": [  
  8.         {  
  9.           "pagePath": "/pages/index/index",  
  10.           "text": "主页",  
  11.           "iconPath": "../../img/tabBar_home.png",  
  12.           "selectedIconPath": "../../img/tabBar_home_cur.png",  
  13.           //"selectedColor": "#4EDF80",  
  14.           active: false  
  15.         },  
  16.         {  
  17.           "pagePath": "/pages/village/city/city",  
  18.           "text": "目的地",  
  19.           "iconPath": "../../../img/tabBar_village.png",  
  20.           "selectedIconPath": "../../../img/tabBar_village_cur.png",  
  21.           "selectedColor": "#4EDF80",  
  22.           active: false  
  23.         },  
  24.         {  
  25.           "pagePath": "/pages/mine/mine",  
  26.           "text": "我的",  
  27.           "iconPath": "../../img/tabBar_mine.png",  
  28.           "selectedIconPath": "../../img/tabBar_mine_cur.png",  
  29.           "selectedColor": "#4EDF80",  
  30.           active: true  
  31.         }  
  32.       ],  
  33.       "position": "bottom"  
  34.     }  
复制代码
mine.wxml引入模板:
  1. <import src="../../template/tabBar.wxml" />  
  2. <template is="tabBar" data="{{tabBar: tabBar}}" />  
复制代码
最后演示如下:
1.gif 
方案二

我把配置数据统一放在app.js文件,通过点击跳转页面后在把数据添加到当前页面实例上,具体做法如下:

    1、app.js文件配置:
  1. //app.js  
  2. var net = require('common/net');  
  3. var a_l, a_d = {}, a_cbSucc, a_cbSuccFail, a_cbFail, a_cbCom, a_h, a_m;  
  4. App({  
  5.   onLaunch: function () {  
  6.     var that = this;  
  7.   },  
  8.   //修改tabBar的active值  
  9.   editTabBar: function () {  
  10.     var _curPageArr = getCurrentPages();  
  11.     var _curPage = _curPageArr[_curPageArr.length - 1];<span style="font-family: Arial, Helvetica, sans-serif;">//相当于Page({})里面的this对象</span>  
  12.     var _pagePath=_curPage.__route__;  
  13.     if(_pagePath.indexOf('/') != 0){  
  14.       _pagePath='/'+_pagePath;  
  15.     }  
  16.     var tabBar=this.globalData.tabBar;  
  17.     for(var i=0; i<tabBar.list.length; i++){  
  18.       tabBar.list.active=false;  
  19.       if(tabBar.list.pagePath==_pagePath){  
  20.         tabBar.list.active=true;//根据页面地址设置当前页面状态  
  21.       }  
  22.     }  
  23.     _curPage.setData({  
  24.       tabBar: tabBar  
  25.     });  
  26.   },  
  27.   globalData: {  
  28.     userInfo: null,  
  29.     //配置tabBar  
  30.     tabBar: {  
  31.       "color": "#9E9E9E",  
  32.       "selectedColor": "#f00",  
  33.       "backgroundColor": "#fff",  
  34.       "borderStyle": "#ccc",  
  35.       "list": [  
  36.         {  
  37.           "pagePath": "/pages/index/index",  
  38.           "text": "主页",  
  39.           "iconPath": "/pages/templateImg/tabBar_home.png",  
  40.           "selectedIconPath": "/pages/templateImg/tabBar_home_cur.png",  
  41.           "selectedColor": "#4EDF80",  
  42.           active: false  
  43.         },  
  44.         {  
  45.           "pagePath": "/pages/village/city/city",  
  46.           "text": "目的地",  
  47.           "iconPath": "/pages/templateImg/tabBar_village.png",  
  48.           "selectedIconPath": "/pages/templateImg/tabBar_village_cur.png",  
  49.           "selectedColor": "#4EDF80",  
  50.           active: false  
  51.         },  
  52.         {  
  53.           "pagePath": "/pages/mine/mine",  
  54.           "text": "我的",  
  55.           "iconPath": "/pages/templateImg/tabBar_mine.png",  
  56.           "selectedIconPath": "/pages/templateImg/tabBar_mine_cur.png",  
  57.           "selectedColor": "#4EDF80",  
  58.           active: false  
  59.         }  
  60.       ],  
  61.       "position": "bottom"  
  62.     }  
  63.   }  
  64. })  
复制代码
2、index.js+mine.js+city.js页面使用:
  1. var App=getApp();  
  2. Page({  
  3.   data:{  
  4.     detail: {},  
  5.   },  
  6.   onLoad:function(options){  
  7.     App.editTabBar();//添加tabBar数据  
  8.     var that=this;  
  9.   }  
  10. })  
复制代码
最终演示和上图一致!


二:map组件markers属性动态初始化

今天在写小程序详情页时候遇到一个问题,map组件的markers属性是通过异步请求到数据后设置的,结果就导致了微信底层渲染出错。但是如果我先在data初始化markers变量,每次小程序都不能正常渲染都是初始化的北京的数据。然后写了如下测试:

test.js:
  1. Page({  
  2.   data:{  
  3.     map:{  
  4.       lat: 0,  
  5.       lng: 0,  
  6.       markers: [],  
  7.       hasMarkers: false//解决方案  
  8.     }  
  9.   },  
  10.   onLoad: function(options){  
  11.     var that=this;  
  12.     wx.getLocation({  
  13.       type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标  
  14.       success: function (res) {  
  15.         // success  
  16.         wx.request({  
  17.           url: 'https://xxx.com/detail.htm?vid=3&latlng=' + res.latitude + ',' + res.longitude,  
  18.           data: {},  
  19.           method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
  20.           // header: {}, // 设置请求的 header  
  21.           success: function (res) {  
  22.             // success  
  23.             that.setData({  
  24.               'map.lat': res.data.data.lat,  
  25.               'map.lng': res.data.data.lng,  
  26.               'map.markers': [{  
  27.                 latitude: res.data.data.lat,  
  28.                 longitude: res.data.data.lng,  
  29.                 name: res.data.data.title,  
  30.                 desc: res.data.data.address  
  31.               }],  
  32.               'map.hasMarkers': true//解决方案  
  33.             });  
  34.             console.log(that.data.map.markers);  
  35.           }  
  36.         })  
  37.       }  
  38.     })  
  39.   }  
  40. });  
复制代码
test.wxml:
  1. <view style="height: 100rpx;">  
  2. {{map.lat}}--{{map.lng}}--{{map.markers[0].name}}--{{map.markers[0].desc}}  
  3. </view>  
  4. <map latitude="{{map.lat}}" longitude="{{map.lng}}" markers="{{map.markers}}"></map>  
复制代码
测试一:data里面没有初始化map变量
2.jpg
测试二:初始化map变量,并在wxml把map数据都打印出来
3.jpg 
为什么会这样呢??各种测试,最后发现只要不是经过远程请求在来设置map值就不会出问题。

后来经大神网友提点,由于异步设置值的过程,组件已经渲染。但是map变量没有预定义是undefined状态,map初始化拿不到数据直接报错了,而异步过来又变成动态更新导致了wxml需要重新渲染map组件的情况导致的。因为官方文档有提到:

地图组件的经纬度必填, 如果不填经纬度则默认值是北京的经纬度。 标记点markers只能在初始化的时候设置,不支持动态更新。

只能初始化一次,因此导致每次都是显示的初始化信息。

然后,网友给出的解决方案完美解决了这个问题:

wx:if会渲染一下组件,那我们按照这个思路给他加个if就行了
  1. <map markers="{{markers}}" style="width: 375px; height: 200px;" wx:if="{{map}}"></map>
复制代码

默认map是false,就是加载时不渲染map组件,等ajax回来后把map设置为true,这样就动态渲染成你要的地址了

原文转自解放号社区:http://bbs.jointforce.com/topic/25439

这篇关于微信小程序编写tabBar模板,map组件markers属性动态初始化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/544492

相关文章

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

Python程序的文件头部声明小结

《Python程序的文件头部声明小结》在Python文件的顶部声明编码通常是必须的,尤其是在处理非ASCII字符时,下面就来介绍一下两种头部文件声明,具有一定的参考价值,感兴趣的可以了解一下... 目录一、# coding=utf-8二、#!/usr/bin/env python三、运行Python程序四、

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢

golang实现动态路由的项目实践

《golang实现动态路由的项目实践》本文主要介绍了golang实现动态路由项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习... 目录一、动态路由1.结构体(数据库的定义)2.预加载preload3.添加关联的方法一、动态路由1

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失