首页 > web前端 > js教程 > 在微信小程序中有关tabBar模板用法(详细教程)

在微信小程序中有关tabBar模板用法(详细教程)

亚连
发布: 2018-06-23 17:29:36
原创
1523 人浏览过

这篇文章主要介绍了微信小程序tabBar模板用法,结合具体实例形式分析了tabBar模板的定义、配置、引用等相关操作技巧,需要的朋友可以参考下

本文实例讲述了微信小程序tabBar模板用法。分享给大家供大家参考,具体如下:

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

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

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

只要新建一个tarBar.wxml模板页,然后引用模板的页面传入数据即可,代码如下:

1

2

3

4

5

6

7

8

9

10

11

<template name="tabBar">

 <view class="flex-h flex-hsb tab-bar" style="color: {{tabBar.color}}; background: {{tarBar.backgroundColor}}; {{tabBar.position==&#39;top&#39;? &#39;top: 0&#39; : &#39;bottom: 0&#39;}}; {{tabBar.borderStyle? (tabBar.position==&#39;top&#39;? &#39;border-bottom: solid 1px &#39;+tabBar.borderStyle + &#39;;&#39; : &#39;border-top: solid 1px &#39;+tabBar.borderStyle + &#39;;&#39;) : &#39;&#39;}}">

 <block wx:for="{{tabBar.list}}" wx:key="pagePath">

  <navigator url="{{item.pagePath}}" open-type="redirect" class="menu-item" style="{{item.active? &#39;color: &#39;+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : &#39;&#39;}}">

   <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的配置对象:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

//配置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引入模板:

1

2

<import src="../../template/tabBar.wxml" />

<template is="tabBar" data="{{tabBar: tabBar}}" />

登录后复制
登录后复制

接下来是mine.js文件引入配置对象:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

//配置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引入模板:

1

2

<import src="../../template/tabBar.wxml" />

<template is="tabBar" data="{{tabBar: tabBar}}" />

登录后复制
登录后复制

最后演示如下:

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

1、app.js文件配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

//app.js

var net = require(&#39;common/net&#39;);

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(&#39;/&#39;) != 0){

   _pagePath=&#39;/&#39;+_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;//根据页面地址设置当前页面状态

   }

  }

  _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页面使用:

1

2

3

4

5

6

7

8

9

10

var App=getApp();

Page({

 data:{

  detail: {},

 },

 onLoad:function(options){

  App.editTabBar();//添加tabBar数据

  var that=this;

 }

})

登录后复制

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在js中如何实现转换时间戳格式

在vue中如何获取dom元素

在vue中如何实现阅读全文

在webpack上如何搭建vue项目

以上是在微信小程序中有关tabBar模板用法(详细教程)的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
微信小程序
来自于 1970-01-01 08:00:00
0
0
0
微信小程序轮播
来自于 1970-01-01 08:00:00
0
0
0
微信小程序自动补全工具
来自于 1970-01-01 08:00:00
0
0
0
现在个人嫩申请微信小程序
来自于 1970-01-01 08:00:00
0
0
0
javascript - 微信小程序如何写倒计时
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板