当前位置:   article > 正文

微信小程序之tabBar

微信小程序之tabBar

1、tabBar

        如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 tab 切换时显示的对应页面。

属性类型必填默认值描述
colorHexColortab 上的文字默认颜色,仅支持十六进制颜色
selectedColorHexColortab 上的文字选中时的颜色,仅支持十六进制颜色
backgroundColorHexColortab 的背景色,仅支持十六进制颜色
borderStylestringblacktabbar 上边框的颜色, 仅支持 black / white
listArraytab 的列表,详见 list 属性说明,最少 2 个、最多 5 个 tab

        其中 list 接受一个数组,只能配置最少 2 个、最多 5 个 tab。tab 按数组的顺序排序,每个项都是一个对象,其属性值如下:

属性类型必填说明
pagePathstring页面路径,必须在 pages 中先定义
textstringtab 上按钮文字
iconPathstring图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。
当 position 为 top 时,不显示 icon。
selectedIconPathstring选中时的图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。
当 position 为 top 时,不显示 icon

在app.json中配置:

  1. "tabBar": {
  2. "backgroundColor":"#F12467",
  3. "list":[
  4. {
  5. "text": "首页",
  6. "pagePath": "pages/navigationBar/navigationBar",
  7. "iconPath": "/static/icon/home.png",
  8. "selectedIconPath": "/static/icon/home-fill.png"
  9. },{
  10. "text": "我的",
  11. "pagePath": "pages/test1/test1",
  12. "iconPath": "/static/icon/user (2).png",
  13. "selectedIconPath": "/static/icon/user.png"
  14. }
  15. ]

图标可以在iconfont-阿里巴巴矢量图标库中获取

2、api中navigate路由接口与组件的关系

urlstring当前小程序内的跳转链接
open-typestring跳转方式 :
合法值说明

navigate

(默认值)

对应 wx.navigateTo 或 wx.navigateToMiniProgram 的功能 
redirect对应 wx.redirectTo 的功能
switchTab对应 wx.switchTab 的功能
reLaunch对应 wx.reLaunch 的功能
navigateBack对应 wx.navigateBack 或 wx.navigateBackMiniProgram (基础库 2.24.4 版本支持)的功能
exit退出小程序,target="miniProgram"时生效

案例1:navigate和reLaunch : 
  1. <navigator url="/pages/showToast/showToast?id=12345&name=lisi" style="margin-bottom: 20rpx;">
  2. go to showToast 非tabBar页面
  3. </navigator>
  4. <!--open-type: 跳转方式,不写的话默认是navigate
  5. reLaunch 和 switchTab都可以进行tabBar页面的跳转,
  6. 但是reLaunch可以携带参数
  7. -->
  8. <navigator url="/pages/wxfor/wxfor?id=12345&name=lisi" open-type="reLaunch">
  9. go to wxfor tabBar页面
  10. </navigator>

案例2: 点击事件进行页面跳转

tabBar页面的话就用wx.reLaunch(Object object)

非tabBar页面就用wx.navigateTo(Object object),因为reLaunch没有左上角的返回箭头:

                        

  1. <view bind:tap="goNavigate" style="width: 200rpx;height: 200rpx;background: chartreuse;">
  2. <view>
  3. <text>
  4. </text>
  5. </view>
  6. <view></view>
  7. </view>
  1. goNavigate(){
  2. // wx.reLaunch({
  3. // url: "/pages/wxfor/wxfor?id=12345&name=lisi"
  4. // })
  5. wx.navigateTo({
  6. url: '/pages/scrollView/scrollView?id=12345&name=lisi',
  7. })
  8. },
 案例3:navigateBack() 回到上一个页面  

3、wx.request获取网络请求并渲染页面 

successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

我们先测试一下这段代码的返回结果:

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. listArr:[]
  7. },
  8. /**
  9. * 生命周期函数--监听页面加载
  10. */
  11. onLoad(options) {
  12. this.getData();
  13. },
  14. getData(){
  15. wx.request({
  16. url: 'https://api.thecatapi.com/v1/images/search?limit=2',
  17. success:res=>{
  18. console.log(res)
  19. this.setData({
  20. listArr:res.data
  21. })
  22. }
  23. })
  24. },

.wxml:

  1. <view class="out">
  2. <view class="box" wx:for="{{listArr}}">
  3. <view class ="pic">
  4. <image src="{{item.url}}" mode="aspectFill" ></image>
  5. </view>
  6. <view class="name">
  7. 姓名是:{{item.id}}
  8. </view>
  9. </view>
  10. </view>

.js:

  1. /* pages/wxRequest/wxRequest.wxss */
  2. .out{
  3. padding:30rpx;
  4. }
  5. .out.box{
  6. border:20px solid rgb(241, 18, 18);
  7. width: 100%;
  8. height: 500rpx;
  9. margin-bottom: 30rpx;
  10. }
  11. .out .box .pic{
  12. width: 100%;
  13. height: 400rpx;
  14. }
  15. .out .box .pic image{
  16. width: 100%;
  17. height: 100%;
  18. }
  19. .out .box .name{
  20. text-align: center;
  21. line-height: 100rpx;
  22. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/261756
推荐阅读
相关标签
  

闽ICP备14008679号