| ylbtech-小程序:配置 |
| 1. 配置返回頂部 |
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"
如 app.json :
{
"window":{
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "微信接口功能演示",
"backgroundColor": "#eeeeee",
"backgroundTextStyle": "light"
}
}

tabBar
如果小程序是一個多 tab 應用(客戶端窗口的底部或頂部有 tab 欄可以切換頁面),可以通過 tabBar 配置項指定 tab 欄的表現,以及 tab 切換時顯示的對應頁面。
Tip:
- 當設置 position 為 top 時,將不會顯示 icon
- tabBar 中的 list 是一個數組,只能配置最少2個、最多5個 tab,tab 按數組的順序排序。
屬性說明:
| 屬性 | 類型 | 必填 | 默認值 | 描述 |
|---|---|---|---|---|
| color | HexColor | 是 | tab 上的文字默認顏色 | |
| selectedColor | HexColor | 是 | tab 上的文字選中時的顏色 | |
| backgroundColor | HexColor | 是 | tab 的背景色 | |
| borderStyle | String | 否 | black | tabbar上邊框的顏色, 僅支持 black/white |
| list | Array | 是 | tab 的列表,詳見 list 屬性說明,最少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的超時時間,單位毫秒,默認為:60000 |
| connectSocket | Number | 否 | wx.connectSocket的超時時間,單位毫秒,默認為:60000 |
| uploadFile | Number | 否 | wx.uploadFile的超時時間,單位毫秒,默認為:60000 |
| downloadFile | Number | 否 | wx.downloadFile的超時時間,單位毫秒,默認為:60000 |
debug
可以在開發者工具中開啟 debug 模式,在開發者工具的控制台面板,調試信息以 info 的形式給出,其信息有Page的注冊,頁面路由,數據更新,事件觸發 。 可以幫助開發者快速定位一些常見的問題。
page.json
每一個小程序頁面也可以使用.json文件來對本頁面的窗口表現進行配置。 頁面的配置比app.json全局配置簡單得多,只是設置 app.json 中的 window 配置項的內容,頁面中配置項會覆蓋 app.json 的 window 中相同的配置項。
頁面的.json只能設置 window 相關的配置項,以決定本頁面的窗口表現,所以無需寫 window 這個鍵,如:
| 屬性 | 類型 | 默認值 | 描述 |
|---|---|---|---|
| navigationBarBackgroundColor | HexColor | #000000 | 導航欄背景顏色,如"#000000" |
| navigationBarTextStyle | String | white | 導航欄標題顏色,僅支持 black/white |
| navigationBarTitleText | String | 導航欄標題文字內容 | |
| backgroundColor | HexColor | #ffffff | 窗口的背景色 |
| backgroundTextStyle | String | dark | 下拉背景字體、loading 圖的樣式,僅支持 dark/light |
| enablePullDownRefresh | Boolean | false | 是否開啟下拉刷新,詳見頁面相關事件處理函數。 |
| disableScroll | Boolean | false | 設置為 true 則頁面整體不能上下滾動;只在 page.json 中有效,無法在 app.json 中設置該項 |
| onReachBottomDistance | Number | 50 | 頁面上拉觸底事件觸發時距頁面底部距離,單位為px |
{
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "微信接口功能演示",
"backgroundColor": "#eeeeee",
"backgroundTextStyle": "light"
}
| 2.返回頂部 |
| 3.返回頂部 |
| 4.返回頂部 |
| 5.返回頂部 |
| 6.返回頂部 |
| 作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 |
