全局配置文件及常用配置项
小程序根目录下的app.js文件是小程序的全局配置文件。常用的配置项如下:
- pages:记录小程序页面存放路径
- window:全局设置小程序外观
- tabBar:设计小程序的底部tabBar效果
- style:是否启用新版的组件样式
小程序配置->全局配置 developers.weixin.qq.com/miniprogram…
小程序窗口的组成部分
window节点中常用配置项
window可配置导航栏区、背景区
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| navigationBarTitleText | string | 导航栏标题文字内容 | |
| navigationBarBackgroundColor | HexColor | #000000 | 导航栏背景颜色,如 #000000 |
| navigationBarTextStyle | string | white | 导航栏标题、状态栏颜色,仅支持 black / white |
| backgroundColor | HexColor | #ffffff | 窗口的背景色 |
| backgroundTextStyle | string | dark | 下拉 loading 的样式,仅支持 dark / light |
| enablePullDownRefresh | boolean | false | 是否开启当前页面下拉刷新。 |
| onReachBottomDistance | number | 50 | 页面上拉触底事件触发时距页面底部距离,单位为px。 |
tabBar
tabBar是移动端应用常见的页面效果,用于实现多页面的快速切换。小程序中通常将其分为:
- 底部tarBar
- 顶部tarBar
注意:
- tarBar中只能配置最少2个、最多5个tab标签
- 当渲染顶部tarBar时,不显示icon,只显示文本
tabBar的6个组成部分
- backgroundColor:tabBar的背景色
- borderStyle:tabBar上边框颜色
- iconPath:未选中时图片的路径
- selectedIconPath:选中是图片的路径
- color:未选中时文字的颜色
- selectedColor:选中时文字的颜色
tabBar配置项
| 属性 | 类型 | 必填 | 默认值 | 描述 |
|---|---|---|---|---|
list | Array | 是 | tab 的列表,最少 2 个、最多 5 个 tab | |
| position | string | 否 | bottom | tabBar 的位置,仅支持 bottom / top |
| backgroundColor | HexColor | 是 | tab 的背景色,仅支持十六进制颜色 | |
| selectedColor | HexColor | 是 | tab 上的文字选中时的颜色,仅支持十六进制颜色 | |
| color | HexColor | 是 | tab 上的文字默认颜色,仅支持十六进制颜色 | |
| borderStyle | string | 否 | black | tabbar 上边框的颜色, 仅支持 black / white |
每个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":{
"list": [
{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath":"images/camera.png",
"selectedIconPath":"images/camera-active.png"
},
{
"pagePath": "pages/day1/day1",
"text": "详情",
"iconPath": "images/tip.png",
"selectedIconPath": "images/tip-active.png"
}
]
},
页面配置和全局配置的关系
app.json中的window节点,可以全局配置小程序中每个页面的窗口表现。
如果某些页面想要特有的窗口表现,此时,页面级别的.json配置文件就可以实现这种需求。
注意:当全局配置和局部配置冲突的时候,根据就近原则,最终的效果以页面的配置为准。
页面配置中常用配置项
developers.weixin.qq.com/miniprogram…
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| navigationBarBackgroundColor | HexColor | #000000 | 导航栏背景颜色,如 #000000 |
| navigationBarTextStyle | string | white | 导航栏标题、状态栏颜色,仅支持 black / white |
| navigationBarTitleText | string | 导航栏标题文字内容 | |
| backgroundTextStyle | string | dark | 下拉 loading 的样式,仅支持 dark / light |
| backgroundColor | HexColor | #ffffff | 窗口的背景色 |
| enablePullDownRefresh | boolean | false | 是否开启当前页面下拉刷新。 详见 Page.onPullDownRefresh |
| onReachBottomDistance | number | 50 | 页面上拉触底事件触发时距页面底部距离,单位为px。 详见 Page.onReachBottom |
注:enablePullDownRefresh不建议全局开启,在使用到的页面开启