《UniApp 页面配置文件pages.json》

🧩 一、pages.json 是什么?

pages.jsonUniApp 项目中非常重要的全局配置文件,用来:

  • 注册页面路径;

  • 定义页面窗口样式;

  • 设置底部 tabBar;

  • 配置路由、导航栏、动画等。

它控制着整个 App 的页面结构和外观。

  • 修改 pages.json 后要重新编译才能生效;

  • 如果页面路径错误,会直接报错 "找不到页面";

  • pages.json 的顺序决定了 首页(第一个页面)

  • 不同平台有些属性不生效(如 gestureBack 只在 App 有效)。

📘 二、基本结构

javascript 复制代码
{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页",
        "navigationBarBackgroundColor": "#ffffff",
        "navigationBarTextStyle": "black"
      }
    },
    {
      "path": "pages/user/user",
      "style": {
        "navigationBarTitleText": "我的"
      }
    }
  ],
  "tabBar": {
    "color": "#666666",
    "selectedColor": "#007AFF",
    "backgroundColor": "#ffffff",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "static/icon/home.png",
        "selectedIconPath": "static/icon/home-active.png"
      },
      {
        "pagePath": "pages/user/user",
        "text": "我的",
        "iconPath": "static/icon/user.png",
        "selectedIconPath": "static/icon/user-active.png"
      }
    ]
  },
  "globalStyle": {
    "navigationBarTextStyle": "white",
    "navigationBarTitleText": "UniApp示例",
    "navigationBarBackgroundColor": "#007AFF",
    "backgroundColor": "#F8F8F8"
  }
}

⚙️ 三、常用配置项讲解

1️⃣ pages(页面注册)

每个页面都必须在这里注册,否则不会被编译。

  • path:页面路径(必须与实际文件路径一致);

  • style:页面样式配置(相当于页面级 page.json)。

2️⃣ style(页面配置)

控制单个页面的窗口表现。

常用属性:

属性名 说明 示例
navigationBarTitleText 导航栏标题 "我的页面"
navigationBarBackgroundColor 导航栏背景色 "#ffffff"
navigationBarTextStyle 导航栏文字颜色 "black""white"
navigationStyle 自定义导航栏 "custom"(用于自定义导航栏)
enablePullDownRefresh 是否开启下拉刷新 true
backgroundColor 页面背景颜色 "#f8f8f8"
backgroundTextStyle 下拉 loading 样式 "dark""light"
disableScroll 禁止页面滚动 true
onReachBottomDistance 触底距离触发加载事件 50
gestureBack 是否开启 iOS 侧滑返回 true

3️⃣ globalStyle(全局默认样式)

对所有页面生效,除非单页覆盖:

javascript 复制代码
"globalStyle": {
  "navigationBarTextStyle": "white",
  "navigationBarBackgroundColor": "#007AFF",
  "backgroundColor": "#F8F8F8"
}

4️⃣ tabBar(底部导航栏)

配置应用底部的固定导航栏:

属性 含义
color 默认文字颜色
selectedColor 选中时文字颜色
backgroundColor 背景颜色
borderStyle 上边框颜色(black/white)
list tab 列表,每项包含 pagePath、text、iconPath、selectedIconPath

5️⃣ 其他重要配置

配置项 说明
subPackages 分包加载配置(用于大项目)
preloadRule 预加载页面
condition 启动调试时的默认启动页面
animationType 页面切换动画类型
animationDuration 页面切换动画时长(ms)

🧱 四、navigationStyle: custom(自定义导航栏)

如果你想使用自定义导航栏(比如封装的 CustomNavBar),要在对应页面的 style 中加上:

javascript 复制代码
{
  "navigationStyle": "custom"
}

⚠️ 这样系统的导航栏会被隐藏,需要自己手动适配状态栏和标题栏高度。

⚠️ 五、单页面 page.json(Vue 文件内)

Vue 页面文件中 ,你也可以写 export default 内的 navigationBarTitleText 配置,但推荐放在 pages.json 里。

相关推荐
农夫山泉不太甜1 小时前
Tauri v2 实战代码示例
前端
消失的旧时光-19431 小时前
Android 面试高频:JSON 文件、大数据存储与断电安全(从原理到工程实践)
android·面试·json
yuhaiqiang1 小时前
被 AI 忽悠后,开始怀念搜索引擎了?
前端·后端·面试
红色石头本尊1 小时前
1-umi-前端工程化搭建
前端
真夜2 小时前
关于对echart盒子设置百分比读取的宽高没有撑开盒子解决方案
前端
楠木6852 小时前
RAG 资料库 Demo 完整开发流程
前端·ai编程
肠胃炎2 小时前
挂载方式部署项目
服务器·前端·nginx
像我这样帅的人丶你还2 小时前
使用 Next.js + Prisma + MySQL 开发全栈项目
前端
FPGA小迷弟2 小时前
FPGA 时序约束基础:从时钟定义到输入输出延迟的完整设置
前端·学习·fpga开发·verilog·fpga
毛骗导演2 小时前
@tencent-weixin/openclaw-weixin 插件深度解析(四):API 协议与数据流设计
前端·架构