微信小程序学习之路《二》 配置

本文介绍了微信小程序的全局配置文件app.json的各项配置项及其用途,包括页面路径设置、窗口表现、底部tab栏配置等内容。

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

 

微信小程序开发 - 配置

 

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

 

配置项表:

属性

类型

必填

描述

pages

Array

设置页面路径

window

Object

设置默认页面的窗口表现

tabBar

Object

设置底部 tab 的表现

networkTimeout

Object

设置网络超时时间

debug

Boolean

设置是否开启 debug 模式

 

 

pages

接受一个数组,每一项都是字符串,代表对应页面的【路径+文件名】信息,文件名不需要写后缀,因为框架会自动去寻找路径.json,.js,.wxml,.wxss的四个文件进行整合。pages属性的第一个值为系统启动页面。小程序中新增、减少页面,都需对pages数组进行修改。

 

{

    "pages":[

        "pages/logs/logs",

        "pages/index/index"

    ]

}

 

 

window

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

属性

类型

默认值

描述

navigationBarBackgroundColor

HexColor

#000000

导航栏背景颜色,如"#000000"

navigationBarTextStyle

String

white

导航栏标题颜色,仅支持 black/white

navigationBarTitleText

String

 

导航栏标题文字内容

backgroundColor

HexColor

#ffffff

窗口的背景色

backgroundTextStyle

String

dark

下拉背景字体、loading图样式,仅支持dark/light

enablePullDownRefresh

Boolean

false

是否开启下拉刷新,详见页面相关事件处理函数。

 

 

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的注册,页面路由,数据更新,事件触发。可以帮助开发者快速定位一些常见的问题。

 

 

参照豆瓣电影微信小程序的一个完整的app.json如下:

{

    "pages": [

        "pages/featured/featured",

        "pages/splash/splash",

        "pages/movie/movie",

        "pages/usbox/usbox",

        "pages/search/search",

        "pages/profile/profile"

    ],

    "window": {

        "navigationBarBackgroundColor": "#000000",

        "navigationBarTextStyle": "white",

        "navigationBarTitleText": "豆瓣电影",

        "backgroundColor": "#ffffff",

        "backgroundTextStyle": "dark",

        "enablePullDownRefresh": false

    },

    "tabBar": {

        "color": "#ccc",

        "selectedColor": "#35495e",

        "borderStyle": "white",

        "backgroundColor": "#fafafa",

        "position": "bottom",

        "list": [

            {

                "text": "推荐电影",

                "pagePath": "pages/featured/featured",

                "iconPath": "images/featured.png",

                "selectedIconPath": "images/featured-actived.png"

            },

            {

                "text": "正在上映",

                "pagePath": "pages/usbox/usbox",

                "iconPath": "images/usbox.png",

                "selectedIconPath": "images/usbox-actived.png"

            },

            {

                "text": "搜索",

                "pagePath": "pages/search/search",

                "iconPath": "images/search.png",

                "selectedIconPath": "images/search-actived.png"

            },

            {

                "text": "资讯",

                "pagePath": "pages/splash/splash",

                "iconPath": "images/news.png",

                "selectedIconPath": "images/news-actived.png"

            },

            {

                "text": "我的",

                "pagePath": "pages/profile/profile",

                "iconPath": "images/profile.png",

                "selectedIconPath": "images/profile-actived.png"

            }

        ]

    },

    "networkTimeout": {

        "request": 10000,

        "connectSocket": 10000,

        "uploadFile": 10000,

        "downloadFile": 10000

    },

    "debug": true

}

 

另外,每一个小程序页面也可以使用page.json文件对本页面的窗口表现进行配置。 页面中配置项会覆盖 app.json的 window 中相同的配置项。因为只能设置 window 相关的配置项所以无需写 window 这个键,如:

 

{

  "navigationBarBackgroundColor": "#ffffff",

  "navigationBarTextStyle": "black",

  "navigationBarTitleText": "微信接口功能演示",

  "backgroundColor": "#eeeeee",

  "backgroundTextStyle": "light"

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值