我們使用app.json文件來對微信小程序進行全局配置,決定頁面文件的路徑、窗口表現、設置網絡超時時間、設置多 tab 等。
以下是一個包含了所有配置選項的簡單配置app.json :{
"pages": [
"pages/index/index",
"pages/logs/index"
],
"window": {
"navigationBarTitleText": "Demo"
},
"tabBar": {
"list": [{
"pagePath": "pages/index/index",
"text": "首頁"
}, {
"pagePath": "pages/logs/logs",
"text": "日誌"
}]
},
"networkTimeout": {
"request": 10000,
"downloadFile": 10000
},
"debug": true
}
app.json 配置項列表
屬性 |
類型 |
必填 |
描述 |
pages |
String Array |
是 |
設置頁面路徑 |
window |
Object |
否 |
設置默認頁面的窗口表現 |
tabBar |
Object |
否 |
設置底部 tab 的表現 |
networkTimeout |
Object |
否 |
設置網絡超時時間 |
debug |
Boolean |
否 |
設置是否開啓 debug 模式 |
pages
接受一個數組,每一項都是字符串,來指定小程序由哪些頁面組成。每一項代表對應頁面的【路徑+文件名】信息,數組的第一項代表小程序的初始頁面。小程序中新增/減少頁面,都需要對 pages 數組進行修改。
文件名不需要寫文件後綴,因爲框架會自動去尋找路徑.json,.js,.wxml,.wxss的四個文件進行整合。
如開發目錄爲:
pages/
pages/index/index.wxml
pages/index/index.js
pages/index/index.wxss
pages/logs/logs.wxml
pages/logs/logs.js
app.js
app.json
app.wxss
則,我們需要在 app.json 中寫
{
"pages":[
"pages/index/index"
"pages/logs/logs"
]
}
window
用於設置小程序的狀態欄、導航條、標題、窗口背景色。
屬性 |
類型 |
默認值 |
描述 |
navigationBarBackgroundColor |
HexColor |
#000000 |
導航欄背景顏色,如"#000000" |
navigationBarTextStyle |
String |
white |
導航欄標題顏色,僅支持 black/white |
navigationBarTitleText |
String |
|
導航欄標題文字內容 |
backgroundColor |
HexColor |
#ffffff |
窗口的背景色 |
backgroundTextStyle |
String |
dark |
下拉背景字體、loading 圖的樣式,僅支持 dark/light |
enablePullDownRefresh |
Boolean |
false |
是否開啓下拉刷新 |
onReachBottomDistance |
Number |
50 |
頁面上拉觸底事件觸發時距頁面底部距離,單位爲px |
注:HexColor(十六進制顏色值),如"#ff00ff"
{
"window":{
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "微信接口功能演示",
"backgroundColor": "#eeeeee",
"backgroundTextStyle": "light"
}
}
tabBar
如果我們的小程序是一個多 tab 應用(客戶端窗口的底部有tab欄可以切換頁面),那麼我們可以通過 tabBar 配置項指定 tab 欄的表現,以及 tab 切換時顯示的對應頁面。
Tip:
1. 當設置 position 爲 top 時,將不會顯示 icon
2. tabBar 是一個數組,只能配置最少2個、最多5個 tab,tab 按數組的順序排序。
屬性說明:
屬性 |
類型 |
必填 |
默認值 |
描述 |
color |
HexColor |
是 |
|
tab 上的文字默認顏色 |
selectedColor |
HexColor |
是 |
|
tab 上的文字選中時的顏色 |
backgroundColor |
HexColor |
是 |
|
tab 的背景色 |
borderStyle |
String |
否 |
black |
tabbar上邊框的顏色, 僅支持 black/white |
list |
Array |
是 |
|
tab 的列表,最少2個、最多5個 tab |
position |
String |
否 |
bottom |
可選值 bottom、top |
其中 list 接受一個數組,數組中的每個項都是一個對象,其屬性值如下:
屬性 |
類型 |
必填 |
說明 |
pagePath |
String |
是 |
頁面路徑,必須在 pages 中先定義 |
text |
String |
是 |
tab 上按鈕文字 |
iconPath |
String |
否 |
圖片路徑,icon 大小限制爲40kb,建議尺寸爲 81px * 81px,當 postion 爲 top 時,此參數無效 |
selectedIconPath |
String |
否 |
選中時的圖片路徑,icon 大小限制爲40kb,建議尺寸爲 81px * 81px ,當 postion 爲 top 時,此參數無效 |
networkTimeout
可以設置各種網絡請求的超時時間。
屬性說明:
屬性 |
類型 |
必填 |
說明 |
request |
Number |
否 |
wx.request的超時時間,單位毫秒 |
connectSocket |
Number |
否 |
wx.connectSocket的超時時間,單位毫秒 |
uploadFile |
Number |
否 |
wx.uploadFile的超時時間,單位毫秒 |
downloadFile |
Number |
否 |
wx.downloadFile的超時時間,單位毫秒 |
debug
可以在開發者工具中開啓 debug 模式,在開發者工具的控制檯面板,調試信息以 info 的形式給出,其信息有Page的註冊,頁面路由,數據更新,事件觸發
。可以幫助開發者快速定位一些常見的問題。