行业知识
微信小程序之tabBar
2026-11-29 06:27:34
来源:互联网转载
1、tabBar
�� 如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 tab 切换时显示的对应页面。
| 属性 | 类型 | 必填 | 默认值 | 描述 |
|---|---|---|---|---|
| color | HexColor | 是 | tab 上的文字默认颜色,仅支持十六进制颜色 | |
| selectedColor | HexColor | 是 | tab 上的文字选中时的颜色,仅支持十六进制颜色 | |
| backgroundColor | HexColor | 是 | tab 的背景色,仅支持十六进制颜色 | |
| borderStyle | string | 否 | black | tabbar 上边框的颜色, 仅支持 black / white |
| list | Array | 是 | tab 的列表,详见 list 属性说明,最少 2 个、最多 5 个 tab |
其中 list 接受一个数组,只能配置最少 2 个、最多 5 个 tab。tab 按数组的顺序排序,每个项都是一个对象,其属性值如下:
| 属性 | 类型 | 必填 | 说明 |
|---|---|---|---|
| pagePath | string | 是 | 页面路径,必须在 pages 中先定义 |
| text | string | 是 | tab 上按钮文字 |
| iconPath | string | 否 | 图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。 当 position 为 top 时,不显示 icon。 |
| selectedIconPath | string | 否 | 选中时的图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。 当 position 为 top 时,不显示 icon |
在app.json中配置:
"tabBar": { "backgroundColor":"#F12467", "list":[ { "text": "首页", "pagePath": "pages/navigationBar/navigationBar", "iconPath": "/static/icon/home.png", "selectedIconPath": "/static/icon/home-fill.png" },{ "text": "我的", "pagePath": "pages/test1/test1", "iconPath": "/static/icon/user (2).png", "selectedIconPath": "/static/icon/user.png" } ] 图标可以在iconfont-阿里巴巴矢量图标库中获取
2、api中navigate路由接口与组件的关系
navigator
| url | string | 当前小程序内的跳转链接 | ||||||||||||
| open-type | string | 跳转方式 : | ||||||||||||
|
案例1:navigate和reLaunch :
go to showToast 非tabBar页面 go to wxfor tabBar页面
案例2: 点击事件进行页面跳转
tabBar页面的话就用wx.reLaunch(Object object)
非tabBar页面就用wx.navigateTo(Object object),因为reLaunch没有左上角的返回箭头:
goNavigate(){ // wx.reLaunch({ // url: "/pages/wxfor/wxfor?id=12345&name=lisi" // }) wx.navigateTo({ url: '/pages/scrollView/scrollView?id=12345&name=lisi', }) }, 案例3:navigateBack() 回到上一个页面
3、wx.request获取网络请求并渲染页面
| success | function | 否 | 接口调用成功的回调函数 |
| fail | function | 否 | 接口调用失败的回调函数 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
我们先测试一下这段代码的返回结果:
Page({ /** * 页面的初始数据 */ data: { listArr:[] }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.getData(); }, getData(){ wx.request({ url: 'https://api.thecatapi.com/v1/images/search?limit=2', success:res=>{ console.log(res) this.setData({ listArr:res.data }) } }) }, .wxml: