Skip to content

6.小程序配置文件

一个小程序应用程序会包括最基本的两种配置文件件。一种是全局的 app.json

和 页面自己的 page.json

注意:配置文件中不能出现注释

6.1. 全局配置app.json

app.json 是当前小程序的全局配置,包括了小程序的所有页面路径、界面表现、网络超时时间、底

部 tab 等。普通快速启动项里边的 app.json 配置

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

完整配置项说明请参考小程序全局配置

以下是一个包含了部分常用配置选项的 app.json

json
{
  "pages": [
    "pages/index/index",
    "pages/logs/index"
  ],
  "window": {
    "navigationBarTitleText": "Demo"
  },
  "tabBar": {
    "list": [{
      "pagePath": "pages/index/index",
      "text": "首页"
    }, {
      "pagePath": "pages/logs/index",
      "text": "日志"
    }]
  },
  "networkTimeout": {
    "request": 10000,
    "downloadFile": 10000
  },
  "debug": true,
  "navigateToMiniProgramAppIdList": [
    "wxe5f52902cf4de896"
  ]

6.1.1 page

用于指定小程序由哪些页面组成,每一项都对应一个页面的 路径(含文件名) 信息。文件名不需要写文件后缀,框架会自动去寻找对应位置的 .json, .js, .wxml, .wxss 四个文件进行处理。

未指定 entryPagePath 时,数组的第一项代表小程序的初始页面(首页)。

小程序中新增/减少页面,都需要对 pages 数组进行修改。

如开发目录为:

├── app.js
├── app.json
├── app.wxss
├── pages
│   │── index
│   │   ├── index.wxml
│   │   ├── index.js
│   │   ├── index.json
│   │   └── index.wxss
│   └── logs
│       ├── logs.wxml
│       └── logs.js
└── utils

则需要在 app.json 中写

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

6.1.2 window

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

属性类型默认值描述最低版本
navigationBarBackgroundColorHexColor#000000导航栏背景颜色,如 #000000,不支持颜色名称
navigationBarTextStylestringwhite导航栏标题颜色,仅支持 black / white
navigationBarTitleTextstring导航栏标题文字内容
navigationStylestringdefault导航栏样式,仅支持以下值: default 默认样式 custom 自定义导航栏,只保留右上角胶囊按钮。参见注 2。微信客户端 6.6.0
backgroundColorHexColor#ffffff窗口的背景色
backgroundTextStylestringdark下拉 loading 的样式,仅支持 dark / light
backgroundColorTopstring#ffffff顶部窗口的背景色,仅 iOS 支持微信客户端 6.5.16
backgroundColorBottomstring#ffffff底部窗口的背景色,仅 iOS 支持微信客户端 6.5.16
enablePullDownRefreshbooleanfalse是否开启全局的下拉刷新。 详见 Page.onPullDownRefresh
onReachBottomDistancenumber50页面上拉触底事件触发时距页面底部距离,单位为 px。 详见 Page.onReachBottom
pageOrientationstringportrait屏幕旋转设置,支持 auto / portrait / landscape 详见 响应显示区域变化2.4.0 (auto) / 2.5.0 (landscape)
  • 注 1:HexColor(十六进制颜色值),如"#ff00ff"

  • 注 2:关于

    navigationStyle
    • 客户端 7.0.0 以下版本,navigationStyle 只在 app.json 中生效。
    • 客户端 6.7.2 版本开始,navigationStyle: customweb-view 组件无效
    • 开启 custom 后,低版本客户端需要做好兼容。开发者工具基础库版本切到 1.7.0(不代表最低版本,只供调试用)可方便切到旧视觉

如:

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

6.1.3 tabBar

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

属性类型必填默认值描述最低版本
colorHexColortab 上的文字默认颜色,仅支持十六进制颜色
selectedColorHexColortab 上的文字选中时的颜色,仅支持十六进制颜色
backgroundColorHexColortab 的背景色,仅支持十六进制颜色
borderStylestringblacktabbar 上边框的颜色, 仅支持 black / white
listArraytab 的列表,详见 list 属性说明,最少 2 个、最多 5 个 tab
positionstringbottomtabBar 的位置,仅支持 bottom / top
custombooleanfalse自定义 tabBar,见详情2.5.0

其中 list 接受一个数组,只能配置最少 2 个、最多 5 个 tab。tab 按数组的顺序排序,每个项都是一个对象,其属性值如下:

属性类型必填说明
pagePathstring页面路径,必须在 pages 中先定义
textstringtab 上按钮文字
iconPathstring图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。 当(../ positiontop 时,不显示 icon。
selectedIconPathstring选中时的图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,不支持网络图片。 positiontop 时,不显示 icon。

官方demo:

自定义页面

代码实现

json
{
    "pages": [
        "pages/demo01/demo01",
        "pages/demo02/demo02",
        "pages/demo03/demo03",
        "pages/demo/index"
    ],
    "window": {
        "backgroundColor": "#F6F6F6",
        "backgroundTextStyle": "light",

        "navigationBarBackgroundColor": "#696",
        "navigationBarTitleText": "云开发 QuickStart",
        "navigationBarTextStyle": "white"
            
    },
    "tabBar": {
        "selectedColor":"#0F0",
      "list": [{
        "pagePath": "pages/demo01/demo01",
        "text": "桌面",
        "iconPath": "images/desktop.png",
        "selectedIconPath": "images/desktop-s.png"
      },
      {
        "pagePath": "pages/demo02/demo02",
        "text": "编辑",
        "iconPath": "images/edit.png",
        "selectedIconPath": "images/edit-s.png"
      },
      {
        "pagePath": "pages/demo03/demo03",
        "text": "钻石",
        "iconPath": "images/diamond.png",
        "selectedIconPath": "images/diamond-s.png"
        
      }]
    },
    "sitemapLocation": "sitemap.json",
    "style": "v2"
}

图片参考:

名字默认图片选中图片
桌面 desktop.pngdesktop-s.png
编辑 edit.pngedit-s.png
钻石 diamond.pngdiamond-s.png

参考矢量图标 FontAwesome

6.2. 页面配置

每一个小程序页面也可以使用同名 .json 文件来对本页面的窗口表现进行配置,页面中配置项会覆盖 app.jsonwindow 中相同的配置项。

完整配置项说明请参考小程序页面配置

例如:

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

6.2.1 配置项

属性类型默认值描述最低版本
navigationBarBackgroundColorHexColor#000000导航栏背景颜色,如 #000000
navigationBarTextStylestringwhite导航栏标题颜色,仅支持 black / white
navigationBarTitleTextstring导航栏标题文字内容
navigationStylestringdefault导航栏样式,仅支持以下值: default 默认样式 custom 自定义导航栏,只保留右上角胶囊按钮微信客户端 7.0.0
backgroundColorHexColor#ffffff窗口的背景色
backgroundTextStylestringdark下拉 loading 的样式,仅支持 dark / light
backgroundColorTopstring#ffffff顶部窗口的背景色,仅 iOS 支持微信客户端 6.5.16
backgroundColorBottomstring#ffffff底部窗口的背景色,仅 iOS 支持微信客户端 6.5.16
enablePullDownRefreshbooleanfalse是否开启当前页面下拉刷新。 详见 Page.onPullDownRefresh
onReachBottomDistancenumber50页面上拉触底事件触发时距页面底部距离,单位为px。 详见 Page.onReachBottom
pageOrientationstringportrait屏幕旋转设置,支持 auto / portrait / landscape 详见 响应显示区域变化2.4.0 (auto) / 2.5.0 (landscape)
disableScrollbooleanfalse设置为 true 则页面整体不能上下滚动。 只在页面配置中有效,无法在 app.json 中设置
usingComponentsObject页面自定义组件配置1.6.3
initialRenderingCachestring页面初始渲染缓存配置2.11.1
stylestringdefault启用新版的组件样式2.10.2
singlePageObject单页模式相关配置2.12.0

页面配置中只能设置 app.jsonwindow 对应的配置项,以决定本页面的窗口表现,所以无需写 window 这个属性。

6.2.2. singlePage

基础库 2.11.3 及以上版本支持,目前分享到朋友圈 (Beta) 后打开会进入单页模式

单页模式相关配置

属性类型必填默认值描述
navigationBarFitString默认自动调整,若原页面是自定义导航栏,则为 float,否则为 squeezed导航栏与页面的相交状态,值为 float 时表示导航栏浮在页面上,与页面相交;值为 squeezed 时表示页面被导航栏挤压,与页面不相交

6.3. sitemap 配置

微信现已开放小程序内搜索,开发者可以通过 sitemap.json 配置,或者管理后台页面收录开关来配置其小程序页面是否允许微信索引。当开发者允许微信索引时,微信会通过爬虫的形式,为小程序的页面内容建立索引。当用户的搜索词条触发该索引时,小程序的页面将可能展示在搜索结果中。 爬虫访问小程序内页面时,会携带特定的 user-agent:mpcrawler场景值1129。需要注意的是,若小程序爬虫发现的页面数据和真实用户的呈现不一致,那么该页面将不会进入索引中。

小程序根目录下的 sitemap.json 文件用于配置小程序及其页面是否允许被微信索引,文件内容为一个 JSON 对象,如果没有 sitemap.json ,则默认为所有页面都允许被索引;sitemap.json 有以下属性:

配置项

属性类型必填描述
rulesObject[]索引规则列表

rules

rules 配置项指定了索引规则,每项规则为一个JSON对象,属性如下所示:

属性类型必填默认值取值取值说明
actionstring"allow""allow"、"disallow"命中该规则的页面是否能被索引
pagestring"*"、页面的路径* 表示所有页面,不能作为通配符使用
paramsstring[][]当 page 字段指定的页面在被本规则匹配时可能使用的页面参数名称的列表(不含参数值)
matchingstring"inclusive"参考 matching 取值说明当 page 字段指定的页面在被本规则匹配时,此参数说明 params 匹配方式
priorityNumber优先级,值越大则规则越早被匹配,否则默认从上到下匹配

matching 取值说明

说明
exact当小程序页面的参数列表等于 params 时,规则命中
inclusive当小程序页面的参数列表包含 params 时,规则命中
exclusive当小程序页面的参数列表与 params 交集为空时,规则命中
partial当小程序页面的参数列表与 params 交集不为空时,规则命中

配置示例

示例1

json
{
  "rules":[{
    "action": "allow",
    "page": "path/to/page",
    "params": ["a", "b"],
    "matching": "exact"
  }, {
    "action": "disallow",
    "page": "path/to/page"
  }]
}
  • path/to/page?a=1&b=2 => 优先索引
  • path/to/page => 不被索引
  • path/to/page?a=1 => 不被索引
  • path/to/page?a=1&b=2&c=3 => 不被索引
  • 其他页面都会被索引

示例2

json
{
  "rules":[{
    "action": "allow",
    "page": "path/to/page",
    "params": ["a", "b"],
    "matching": "inclusive"
  }, {
    "action": "disallow",
    "page": "path/to/page"
  }]
}
  • path/to/page?a=1&b=2 => 优先索引
  • path/to/page?a=1&b=2&c=3 => 优先索引
  • path/to/page => 不被索引
  • path/to/page?a=1 => 不被索引
  • 其他页面都会被索引

示例3

json
{
  "rules":[{
    "action": "allow",
    "page": "path/to/page",
    "params": ["a", "b"],
    "matching": "exclusive"
  }, {
    "action": "disallow",
    "page": "path/to/page"
  }]
}
  • path/to/page => 优先索引
  • path/to/page?c=3 => 优先索引
  • path/to/page?a=1 => 不被索引
  • path/to/page?a=1&b=2 => 不被索引
  • 其他页面都会被索引

示例4

json
{
  "rules":[{
    "action": "allow",
    "page": "path/to/page",
    "params": ["a", "b"],
    "matching": "partial"
  }, {
    "action": "disallow",
    "page": "path/to/page"
  }]
}
  • path/to/page?a=1 => 优先索引
  • path/to/page?a=1&b=2 => 优先索引
  • path/to/page => 不被索引
  • path/to/page?c=3 => 不被索引
  • 其他页面都会被索引

注:没有 sitemap.json 则默认所有页面都能被索引

注:{"action": "allow", "page": "\*"} 是优先级最低的默认规则,未显式指明 "disallow" 的都默认被索引

6.4 作业

  1. 配置一个tabBar分别提供三个选项,点击不同的选项查看不同的页面

Released under the MIT License.