您的位置:首页 > 移动开发 > 微信开发

微信小程序开发简单教程---配置

2018-01-25 09:18 501 查看
最近有空自学了下微信小程序,以下教程仅供自己日后查看用。

1.配置

app.json
文件用来对微信小程序进行全局配置,决定页面文件的路径、窗口表现、设置网络超时时间、设置多 tab 等。

* 注意:在App.json中不能添加任何注释。

属性类型必填描述
pagesString Array设置页面路径
windowObject设置默认页面的窗口表现
tabBarObject设置底部 tab 的表现
networkTimeoutObject设置网络超时时间
debugBoolean设置是否开启 debug 模式
1.1  pages

接受一个数组,每一项都是字符串,来指定小程序由哪些页面组成。每一项代表对应页面的【路径+文件名】信息,数组的第一项代表小程序的初始页面。小程序中新增/减少页面,都需要对 pages 数组进行修改。

文件名不需要写文件后缀,因为框架会自动去寻找路径下
.json
,
.js
,
.wxml
,
.wxss
四个文件进行整合。

eg: 

{
"pages":[
"pages/index/index",
"pages/logs/logs"
]
}


1.2  window

用于设置小程序的状态栏、导航条、标题、窗口背景色。

属性类型默认值描述
navigationBarBackgroundColorHexColor#000000导航栏背景颜色,如"#000000"
navigationBarTextStyleStringwhite导航栏标题颜色,仅支持 black/white
navigationBarTitleTextString 导航栏标题文字内容
backgroundColorHexColor#ffffff窗口的背景色
backgroundTextStyleStringdark下拉背景字体、loading 图的样式,仅支持 dark/light
enablePullDownRefreshBooleanfalse是否开启下拉刷新,详见页面相关事件处理函数
onReachBottomDistanceNumber50页面上拉触底事件触发时距页面底部距离,单位为px
注:HexColor(十六进制颜色值),如"#ff00ff"
eg:

{
"window":{
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "微信接口功能演示",
"backgroundColor": "#eeeeee",
"backgroundTextStyle": "light"
}
}


1.3 tabBar

如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 tab 切换时显示的对应页面。

Tip:

当设置 position 为 top 时,将不会显示 icon
tabBar 中的 list 是一个数组,只能配置最少2个、最多5个 tab,tab 按数组的顺序排序。
属性说明:

属性类型必填默认值描述
colorHexColor tab 上的文字默认颜色
selectedColorHexColor tab 上
4000
的文字选中时的颜色
backgroundColorHexColor tab 的背景色
borderStyleStringblacktabbar上边框的颜色, 仅支持 black/white
listArray tab 的列表,详见 list 属性说明,最少2个、最多5个 tab
positionStringbottom可选值 bottom、top
其中 list 接受一个数组,数组中的每个项都是一个对象,其属性值如下:

属性类型必填说明
pagePathString页面路径,必须在 pages 中先定义
textStringtab 上按钮文字
iconPathString图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px,当 postion 为 top 时,此参数无效,不支持网络图片
selectedIconPathString选中时的图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px ,当 postion 为 top 时,此参数无效
eg:

"tabBar": {
"color":"#7A7E83",
"selectedColor":"#3cc51f",
"backgroundColor" :"#F7F7FA",
"borderStyle":"white",
"list": [{
"pagePath":"pages/word/word",
"text":"背词",
"iconPath":"images/home.png",
"selectedIconPath":"images/home-selected.png"
}


1.4 networkTimeout

可以设置各种网络请求的超时时间。

属性说明:

属性类型必填说明
requestNumberwx.request的超时时间,单位毫秒,默认为:60000
connectSocketNumberwx.connectSocket的超时时间,单位毫秒,默认为:60000
uploadFileNumberwx.uploadFile的超时时间,单位毫秒,默认为:60000
downloadFileNumberwx.downloadFile的超时时间,单位毫秒,默认为:60000
1.5 debug
可以在开发者工具中开启 debug 模式,在开发者工具的控制台面板,调试信息以 info 的形式给出,其信息有
Page的注册
页面路由
数据更新
事件触发
。 可以帮助开发者快速定位一些常见的问题。

1.6 page.json

每一个小程序页面也可以使用
.json
文件来对本页面的窗口表现进行配置。 页面的配置比
app.json
全局配置简单得多,只是设置 app.json 中的 window 配置项的内容,页面中配置项会覆盖 app.json 的 window 中相同的配置项。

页面的
.json
只能设置
window
相关的配置项,以决定本页面的窗口表现,所以无需写
window
这个键,如:

属性类型默认值描述
navigationBarBackgroundColorHexColor#000000导航栏背景颜色,如"#000000"
navigationBarTextStyleStringwhite导航栏标题颜色,仅支持 black/white
navigationBarTitleTextString 导航栏标题文字内容
backgroundColorHexColor#ffffff窗口的背景色
backgroundTextStyleStringdark下拉背景字体、loading 图的样式,仅支持 dark/light
enablePullDownRefreshBooleanfalse是否开启下拉刷新,详见页面相关事件处理函数
disableScrollBooleanfalse设置为 true 则页面整体不能上下滚动;只在 page.json 中有效,无法在 app.json 中设置该项
onReachBottomDistanceNumber50页面上拉触底事件触发时距页面底部距离,单位为px
eg:

{
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "微信接口功能演示",
"backgroundColor": "#eeeeee",
"backgroundTextStyle": "light"
}


下面展示下项目中的app.json的完整样式:

{
"pages":[
"pages/word/word",
"pages/search/search",
"pages/settings/settings",
"pages/search/detail/detail",
"pages/settings/clause/clause",
"pages/settings/help/help",
"pages/settings/feedback/feedback",
"pages/note/index/index",
"pages/note/create/index",
"pages/note/edit/index",
"pages/intro/intro",
"pages/intro/trick/trick",
"pages/intro/recommend/recommend",
"pages/intro/saying/saying"

],
"tabBar": {
"color": "#7A7E83",
"selectedColor": "#3cc51f",
"backgroundColor" : "#F7F7FA",
"borderStyle": "white",
"list": [{
"pagePath": "pages/word/word",
"text": "背词",
"iconPath": "images/home.png",
"selectedIconPath": "images/home-selected.png"
},
{
"pagePath": "pages/search/search",
"text": "查词",
"iconPath": "images/search.png",
"selectedIconPath": "images/search-selected.png"
},
{
"pagePath": "pages/intro/intro",
"text": "方法",
"iconPath": "images/intro.png",
"selectedIconPath": "images/intro-selected.png"
},
{
"pagePath": "pages/note/index/index",
"text": "笔记",
"iconPath": "images/note.png",
"selectedIconPath": "images/note-selected.png"
},
{
"pagePath": "pages/settings/settings",
"text": "设置",
"iconPath": "images/settings.png",
"selectedIconPath": "images/settings-selected.png"
}]
},
"window":{
"backgroundTextStyle":"light",
"backgroundColor": "#EFEFF4",
"navigationBarBackgroundColor": "#2C2D31",
"navigationBarTitleText": "佑米单词",
"navigationBarTextStyle":"white",
"enablePullDownRefresh": true
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: