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

这篇教程介绍了微信小程序的配置,包括App.json中页面路径的设定、全局窗口配置、tabBar的配置以及网络超时时间和debug模式的设置。还提及了page.json用于本页面窗口表现的配置,强调了页面配置会覆盖全局配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近有空自学了下微信小程序,以下教程仅供自己日后查看用。

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:

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

属性说明:

属性类型必填默认值描述
colorHexColor tab 上的文字默认颜色
selectedColorHexColor tab 上的文字选中时的颜色
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
  }
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值