uni-app 配置文件 pages.json

pages.json

pages.json 文件用来对 uni-app 进行全局配置,决定页面文件的路径、窗口表现、设置多 tab 等。

pages.json 配置项列表

属性名

是否必填

说明

globalStyle Object


设置默认页面的窗口表现

pages Object Array


设置页面路径及窗口表现

tabBar Object


设置底部 tab 的表现

condition Object


启动模式配置

以下是一个包含了所有配置选项的 pages.json :

{
  "pages": [{
    "path": "pages/component/index",
    "style": {
      "navigationBarTitleText": "组件"
    }
  }, {
    "path": "pages/API/index",
    "style": {
      "navigationBarTitleText": "接口"
    }
  }, {
    "path": "pages/component/view/index",
    "style": {
      "navigationBarTitleText": "view"
    }
  }],
  "globalStyle": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "演示",
    "navigationBarBackgroundColor": "#F8F8F8",
    "backgroundColor": "#F8F8F8"
  },
  "tabBar": {
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [{
      "pagePath": "pages/component/index",
      "iconPath": "static/image/icon_component.png",
      "selectedIconPath": "static/image/icon_component_HL.png",
      "text": "组件"
    }, {
      "pagePath": "pages/API/index",
      "iconPath": "static/image/icon_API.png",
      "selectedIconPath": "static/image/icon_API_HL.png",
      "text": "接口"
    }]
  }
}

globalStyle

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

属性名

类型

默认值

说明

avigationBarBackgroundColor

HexColor

#000000

导航栏背景颜色

navigationBarTextStyle

String

white

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

navigationBarTitleText

String

导航栏标题文字内容

navigationStyle

String

default

导航栏样式,仅支持 default/custom。

backgroundColor

HexColor

#ffffff

窗口的背景色 微信小程序

注意:navigationStyle 只在 pages.json->globalStyle 中设置生效。开启 custom 后,所有窗口均无导航栏。


pages

接收一个数组,来指定应用由哪些页面组成。每一项代表对应页面的信息,应用中新增/减少页面,都需要对 pages 数组进行修改。 文件名不需要写后缀,框架会自动寻找路径下的页面资源。
注意:pages节点的第一项为应用入口页(即首页)。

代码示例:
开发目录为时:

pages/
pages/index/index.vue
pages/login/login.vue
mainfest.json
pages.json

则需要在 pages.json 中填写

{
  "pages": [{
      "index": {
        "path": "pages/index/index"
      }
    },
    {
      "login": {
        "path": "pages/login/login"
      }
    }
  ]
}

pages.style

用于设置每个页面的状态栏、导航条、标题、窗口背景色等。

属性名

类型

默认值

说明

navigationBarBackgroundColor

HexColor

#000000

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

navigationBarTextStyle

String

white

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

navigationBarTitleText

String

导航栏标题文字内容

backgroundColor

HexColor

#ffffff

窗口的背景色 微信小程序

backgroundTextStyle

String

dark

下拉 loading 的样式,仅支持 dark/light

enablePullDownRefresh

Boolean

false

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

onReachBottomDistance

Number

50

页面上拉触底事件触发时距页面底部距离,单位为px

navigationStyle

String

default

导航栏样式,仅支持 default/custom。custom 模式可自定义导航栏,只保留右上角胶囊状的按钮。 微信小程序

backgroundColorTop

String

#ffffff

顶部窗口的背景色。 微信小程序且为 iOS

backgroundColorBottom

String

#ffffff

底部窗口的背景色。 微信小程序且为 iOS

代码示例:

{
  "pages": [{
      "index": {
        "path": "pages/index/index",
        "style": {
            "navigationBarTitleText": "首页",//设置页面标题文字
          "enablePullDownRefresh":true//开启下拉刷新
        }
      }
    },
    ...
  ]
}

tabBar

如果应用是一个多 tab 应用,可以通过 tabBar 配置项指定 tab 栏的表现,以及 tab 切换时显示的对应页。

Tips

当设置 position 为 top 时,将不会显示 icon
tabBar 中的 list 是一个数组,只能配置最少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

position

String


默认值为:bottom 可选值 bottom、top

其中 list 接收一个数组,数组中的每个项都是一个对象,其属性值如下:

属性名

类型

是否必填

说明

pagePath

String


页面路径,必须在 pages 中先定义

text

String


tab 上按钮文字

iconPath

String


图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px,当 postion 为 top 时,此参数无效,不支持网络图片

selectedIconPath

String


选中时的图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px ,当 postion 为 top 时,此参数无效


condition

启动模式配置,仅开发期间生效,用于模拟直达页面的场景,如:小程序转发后,用户点击所打开的页面。

属性说明:

属性名

类型

是否必填

说明

current

Number


当前激活的模式,list节点的索引值

list

y


启动模式列表

list说明:

属性名

类型

是否必填

说明

name

String


启动模式名称

path

ring


启动页面路径

query

String


启动参数,可在页面的 onLoad 函数里获得

注意: 在5+app里真机运行可直接打开配置的页面,微信开发者工具里需要手动改变编译模式, 代码示例:

"condition": { //模式配置,仅开发期间生效
    "current": 0, //当前激活的模式(list 的索引项)
    "list": [{
            "name": "swiper", //模式名称
            "path": "pages/component/swiper/swiper", //启动页面,必选
            "query": "interval=4000&autoplay=false" //启动参数,在页面的onLoad函数里面得到。
        },
        {
            "name": "test",
            "path": "pages/component/switch/switch"
        }
    ]
}

注意

1.注意:navigationStyle 只在 pages.json->globalStyle 中设置生效。开启 custom 后,所有窗口均无导航栏
2.注意: 在5+app里真机运行可直接打开配置的页面,微信开发者工具里需要手动改变编译模式, 代码示例
3.注意:pages节点的第一项为应用入口页(即首页)。


总结

本篇文章主要介绍了 uni-app 配置文件之 pages.json 和 pages.json的属性介绍,
如有任何疑问请移步官网