- 微信小程序开发与运营
- 李文奎
- 992字
- 2025-02-17 12:40:27
2.4 配置文件
小程序的配置文件按其作用范围可以分为全局配置文件(app. json)和页面配置文件(∗. json)。全局配置文件作用于整个小程序,页面配置文件只作用于当前页面。由于页面配置文件的优先级高于全局配置文件的优先级,因此,当全局配置文件与页面配置文件有相同配置项时,页面配置文件会覆盖全局配置文件中的相同配置项内容。
2.4.1 全局配置文件
小程序的全局配置保存在全局配置文件(app. json)中,使用全局配置文件来配置页面文件(pages)的路径、设置窗口(window)表现、设定网络请求API的超时时间值(networkTimeout)以及配置多个切换页(tabBar)等。表2-2列出了各全局配置项的相关描述。
表2-2 全局配置项

全局配置文件内容的整体结构如下:

1. pages配置项
pages配置项接受一个数组,用来指定小程序由哪些页面组成,数组的每一项都是字符串,代表对应页面的“路径”+“文件名”。pages配置项是必填项。
设置pages配置项时,应注意以下3点:
(1)数组的第一项用于设定小程序的初始页面。
(2)在小程序中新增或减少页面时,都需要对数组进行修改。
(3)文件名不需要写文件扩展名。小程序框架会自动寻找路径及对.js、. json、. wxml和.wxss文件进行整合数据绑定。
例如,app. json文件的配置如下:

2. window配置项
window配置项负责设置小程序状态栏、导航条、标题、窗口背景色等系统样式。window配置项可以配置的对象参考表2-3。
表2-3 window配置项及其描述

在app. json中设置如下window配置项:

3. tabBar配置项
当需要在程序顶部或底部设置菜单栏时,可以通过配置tabBar配置项来实现。tabBar配置项可以配置的属性如表2-4所示。
表2-4 tabBar配置项及其描述

其中,list(列表)接受数组值,数组中的每一项也都是一个对象。对象的数据值说明如表2-5所示。
表2-5 tabBar中list选项

在app. json文件中设置如下tabBar配置:

配置后的页面效果如图2-9所示。

图2-9 tabBar标签页
4. networkTimeout配置项
小程序中各种网络请求API的超时时间值只能通过networkTimeout配置项进行统一设置,不能在API中单独设置。networkTimeout可以配置的属性如表2-6所示。
表2-6 networkTimeout配置项

例如,为提高网络响应效率,开发者可以在app. json中使用下列超时设置:

5. debug配置项
debug配置项用于开启开发者工具的调试模式,默认为false。开启后,页面的注册、路由、数据更新、事件触发等调试信息将以info的形式输出到Console(控制台)面板上。
2.4.2 页面配置文件
页面配置文件(∗. json)只能设置本页面的窗口表现,而且只能设置window配置项的内容。在配置页面配置文件后,页面中的window配置值将覆盖全局配置文件(app. json)中的配置值。
页面中的window配置只需书写配置项,不必书写window,代码示例如下:
