微信小程序——配置
以下就是小編對小程序配置的資料進行的系統的整理,希望能對開發者有幫助。
我們使用app.json文件來對微信小程序進行全局配置,決定頁面文件的路徑、窗口表現、設置網絡超時時間、設置多 tab 等。
以下是一個包含了所有配置選項的簡單配置app.json :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
{
"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 | Array | 是 | 設置頁面路徑 |
window | Object | 否 | 設置默認頁面的窗口表現 |
tabBar | Object | 否 | 設置底部 tab 的表現 |
networkTimeout | Object | 否 | 設置網絡超時時間 |
debug | Boolean | 否 | 設置是否開啟 debug 模式 |
pages
接受一個數組,每一項都是字符串,來指定小程序由哪些頁面組成。每一項代表對應頁面的【路徑+文件名】信息,數組的第一項代表小程序的初始頁面。小程序中新增/減少頁面,都需要對 pages 數組進行修改。
文件名不需要寫文件后綴,因為框架會自動去尋找路徑.json,.js,.wxml,.wxss的四個文件進行整合。
如開發目錄為:
1
2
3
4
5
6
7
8
9
|
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 中寫
1
2
3
4
5
6
|
{
"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 |
注:HexColor(十六進制顏色值),如"#ff00ff"
如 app.json :
1
2
3
4
5
6
7
8
9
|
{
"window"
:{
"navigationBarBackgroundColor"
:
"#ffffff"
,
"navigationBarTextStyle"
:
"black"
,
"navigationBarTitleText"
:
"微信接口功能演示"
,
"backgroundColor"
:
"#eeeeee"
,
"backgroundTextStyle"
:
"light"
}
}
|
tabBar
如果我們的小程序是一個多 tab 應用(客戶端窗口的底部有tab欄可以切換頁面),那么我們可以通過 tabBar 配置項指定 tab 欄的表現,以及 tab 切換時顯示的對應頁面。
tabBar 是一個數組,只能配置最少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 |
其中 list 接受一個數組,數組中的每個項都是一個對象,其屬性值如下:
屬性 | 類型 | 必填 | 說明 |
---|---|---|---|
pagePath | String | 是 | 頁面路徑,必須在 pages 中先定義 |
text | String | 是 | tab 上按鈕文字 |
iconPath | String | 是 | 圖片路徑,icon 大小限制為40kb |
selectedIconPath | String | 是 | 選中時的圖片路徑,icon 大小限制為40kb |
networkTimeout
可以設置各種網絡請求的超時時間。
屬性說明:
屬性 | 類型 | 必填 | 說明 |
---|---|---|---|
request | Number | 否 | wx.request的超時時間,單位毫秒 |
connectSocket | Number | 否 | wx.connectSocket的超時時間,單位毫秒 |
uploadFile | Number | 否 | wx.uploadFile的超時時間,單位毫秒 |
downloadFile | Number | 否 | wx.downloadFile的超時時間,單位毫秒 |
debug
可以在開發者工具中開啟 debug 模式,在開發者工具的控制台面板,調試信息以 info 的形式給出,其信息有Page的注冊,頁面路由,數據更新,事件觸發 。 可以幫助開發者快速定位一些常見的問題。
page.json
每一個小程序頁面也可以使用.json文件來對本頁面的窗口表現進行配置。 頁面的配置比app.json全局配置簡單得多,只是設置 app.json 中的 window 配置項的內容,頁面中配置項會覆蓋 app.json 的 window 中相同的配置項。
頁面的.json只能設置 window 相關的配置項,以決定本頁面的窗口表現,所以無需寫 window 這個鍵,如:
1
2
3
4
5
6
7
|
{
"navigationBarBackgroundColor"
:
"#ffffff"
,
"navigationBarTextStyle"
:
"black"
,
"navigationBarTitleText"
:
"微信接口功能演示"
,
"backgroundColor"
:
"#eeeeee"
,
"backgroundTextStyle"
:
"light"
}
|
{ "pages":[ "pages/index/index", "pages/logs/logs" ], "tabBar": { "color":"red", "selectedColor":"blue", "backgroundColor":"#666", "borderStyle":"red", "list": [{ "pagePath": "pages/index/index", "text": "首頁", "iconPath" "selectedIconPath" }, { "pagePath": "pages/logs/logs", "text": "日志" }] }, "window":{ "backgroundTextStyle":"light", "navigationBarBackgroundColor": "#3a3b3f", "navigationBarTitleText": "樹懶找房", "navigationBarTextStyle":"#fff" } }