uni-app pages.json文件中条件编译代码报错(编译到支付宝端报错)控制台报错

发布于 1周前 作者 caililin 来自 Uni-App

uni-app pages.json文件中条件编译代码报错(编译到支付宝端报错)控制台报错

开发环境 版本号 项目创建方式
Windows 13.2 HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Windows

HBuilderX类型:正式

HBuilderX版本号:3.98

手机系统:Android

手机系统版本号:Android 12

手机厂商:小米

手机机型:小米10

页面类型:vue

vue版本:vue2

打包方式:云端

操作步骤:

  • pages.json文件中创建条件编译代码

预期结果:

  • 不报错

实际结果:

  • 报错 /Users/haiye/Documents/project/work/wx_miniprogram/pages.json: line 169, col 17, Duplicate key ‘navigationBarTitleText’.

bug描述:

  • 报错信息
    • /Users/haiye/Documents/project/work/wx_miniprogram/pages.json: line 169, col 17, Duplicate key ‘navigationBarTitleText’.
  • 我的条件编译代码
    {
        "path": "pages/personal/phoneStagingOrder/phoneStagingOrder",
        "style": {
            // #ifdef MP-TOUTIAO
            "navigationBarTitleText": "我的订单",
            // #endif
            // #ifndef MP-TOUTIAO
            "navigationBarTitleText": "手机订单",
            // #endif
            "enablePullDownRefresh": false
        }
    }

6 回复

会有波浪线的错误提示

目前先删除代码,编译完成后,后续再改回来

感谢反馈,现在还有问题吗,新版本修复了条件编译的问题

新版本可以了

uni-app 中,pages.json 文件用于配置页面的路由和窗口表现等。条件编译是通过 #ifdef#endif 来实现的,允许你在不同平台上编译不同的代码。如果你在 pages.json 文件中使用条件编译时,编译到支付宝端报错,可能是以下原因导致的:

1. 条件编译语法错误

确保你的条件编译语法是正确的。例如:

{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页"
      }
    }
  ],
  "condition": {
    "current": 0,
    "list": [
      {
        "name": "支付宝",
        "path": "pages/alipay/index"
      }
    ]
  },
  "globalStyle": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "uni-app",
    "navigationBarBackgroundColor": "#F8F8F8",
    "backgroundColor": "#F8F8F8"
  },
  "tabBar": {
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      }
    ]
  },
  "usingComponents": {
    "my-component": "/components/my-component"
  },
  "subPackages": [
    {
      "root": "pages/sub",
      "pages": [
        {
          "path": "sub/index",
          "style": {
            "navigationBarTitleText": "子页面"
          }
        }
      ]
    }
  ],
  "preloadRule": {
    "pages/index/index": {
      "network": "all",
      "packages": ["pages/sub"]
    }
  },
  "plugins": {
    "myPlugin": {
      "version": "1.0.0",
      "provider": "wxidxxxxxxxxxxxxxx"
    }
  },
  "workers": "workers",
  "requiredBackgroundModes": ["audio"],
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "sitemapLocation": "sitemap.json",
  "style": "v2",
  "useExtendedLib": {
    "kbone": true
  },
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "networkTimeout": {
    "request": 60000,
    "connectSocket": 60000,
    "uploadFile": 60000,
    "downloadFile": 60000
  },
  "debug": true,
  "functionalPages": true,
  "resizable": true,
  "navigateToMiniProgramAppIdList": [
    "wxidxxxxxxxxxxxxxx"
  ],
  "usingComponents": {
    "my-component": "/components/my-component"
  },
  "subPackages": [
    {
      "root": "pages/sub",
      "pages": [
        {
          "path": "sub/index",
          "style": {
            "navigationBarTitleText": "子页面"
          }
        }
      ]
    }
  ],
  "preloadRule": {
    "pages/index/index": {
      "network": "all",
      "packages": ["pages/sub"]
    }
  },
  "plugins": {
    "myPlugin": {
      "version": "1.0.0",
      "provider": "wxidxxxxxxxxxxxxxx"
    }
  },
  "workers": "workers",
  "requiredBackgroundModes": ["audio"],
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "sitemapLocation": "sitemap.json",
  "style": "v2",
  "useExtendedLib": {
    "kbone": true
  },
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "networkTimeout": {
    "request": 60000,
    "connectSocket": 60000,
    "uploadFile": 60000,
    "downloadFile": 60000
  },
  "debug": true,
  "functionalPages": true,
  "resizable": true,
  "navigateToMiniProgramAppIdList": [
    "wxidxxxxxxxxxxxxxx"
  ]
}

2. 平台标识符错误

确保你使用的平台标识符是正确的。例如,支付宝小程序的平台标识符是 MP-ALIPAY,而不是 ALIPAY 或其他。

{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页"
      }
    }
  ],
  "condition": {
    "current": 0,
    "list": [
      {
        "name": "支付宝",
        "path": "pages/alipay/index"
      }
    ]
  },
  "globalStyle": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "uni-app",
    "navigationBarBackgroundColor": "#F8F8F8",
    "backgroundColor": "#F8F8F8"
  },
  "tabBar": {
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      }
    ]
  },
  "usingComponents": {
    "my-component": "/components/my-component"
  },
  "subPackages": [
    {
      "root": "pages/sub",
      "pages": [
        {
          "path": "sub/index",
          "style": {
            "navigationBarTitleText": "子页面"
          }
        }
      ]
    }
  ],
  "preloadRule": {
    "pages/index/index": {
      "network": "all",
      "packages": ["pages/sub"]
    }
  },
  "plugins": {
    "myPlugin": {
      "version": "1.0.0",
      "provider": "wxidxxxxxxxxxxxxxx"
    }
  },
  "workers": "workers",
  "requiredBackgroundModes": ["audio"],
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "sitemapLocation": "sitemap.json",
  "style": "v2",
  "useExtendedLib": {
    "kbone": true
  },
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "networkTimeout": {
    "request": 60000,
    "connectSocket": 60000,
    "uploadFile": 60000,
    "downloadFile": 60000
  },
  "debug": true,
  "functionalPages": true,
  "resizable": true,
  "navigateToMiniProgramAppIdList": [
    "wxidxxxxxxxxxxxxxx"
  ],
  "usingComponents": {
    "my-component": "/components/my-component"
  },
  "subPackages": [
    {
      "root": "pages/sub",
      "pages": [
        {
          "path": "sub/index",
          "style": {
            "navigationBarTitleText": "子页面"
          }
        }
      ]
    }
  ],
  "preloadRule": {
    "pages/index/index": {
      "network": "all",
      "packages": ["pages/sub"]
    }
  },
  "plugins": {
    "myPlugin": {
      "version": "1.0.0",
      "provider": "wxidxxxxxxxxxxxxxx"
    }
  },
  "workers": "workers",
  "requiredBackgroundModes": ["audio"],
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "sitemapLocation": "sitemap.json",
  "style": "v2",
  "useExtendedLib": {
    "kbone": true
  },
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "networkTimeout": {
    "request": 60000,
    "connectSocket": 60000,
    "uploadFile": 60000,
    "downloadFile": 60000
  },
  "debug": true,
  "functionalPages": true,
  "resizable": true,
  "navigateToMiniProgramAppIdList": [
    "wxidxxxxxxxxxxxxxx"
  ]
}

3. JSON 格式错误

确保 pages.json 文件的 JSON 格式是正确的,没有多余的逗号或缺少的括号。

4. 支付宝小程序不支持某些配置

支付宝小程序可能不支持 pages.json 中的某些配置项。你可以查阅支付宝小程序的官方文档,确认哪些配置项是支持的。

5. 控制台报错信息

查看控制台报错信息,通常会有具体的错误提示。根据错误提示来定位问题。

6. 使用 uni-app 官方文档

参考 uni-app 官方文档中关于条件编译和 pages.json 的说明,确保你使用的语法和配置是正确的。

示例代码

以下是一个简单的 pages.json 文件,包含条件编译的示例:

{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页"
      }
    }
  ],
  "globalStyle": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "uni-app",
    "navigationBarBackgroundColor": "#F8F8F8",
    "backgroundColor": "#F8F8F8"
  },
  "tabBar": {
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      }
    ]
  },
  "usingComponents": {
    "my-component": "/components/my-component"
  },
  "subPackages": [
    {
      "root": "pages/sub",
      "pages": [
        {
          "path": "sub/index",
          "style": {
            "navigationBarTitleText": "子页面"
          }
        }
      ]
    }
  ],
  "preloadRule": {
    "pages/index/index": {
      "network": "all",
      "packages": ["pages/sub"]
    }
  },
  "plugins": {
    "myPlugin": {
      "version": "1.0.0",
      "provider": "wxidxxxxxxxxxxxxxx"
    }
  },
  "workers": "workers",
  "requiredBackgroundModes": ["audio"],
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "sitemapLocation": "sitemap.json",
  "style": "v2",
  "useExtendedLib": {
    "kbone": true
  },
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "networkTimeout": {
    "request": 60000,
    "connectSocket": 60000,
    "uploadFile": 60000,
    "downloadFile": 60000
  },
  "debug": true,
  "functionalPages": true,
  "resizable": true,
  "navigateToMiniProgramAppIdList": [
    "wxidxxxxxxxxxxxxxx"
  ],
  "usingComponents": {
    "my-component": "/components/my-component"
  },
  "subPackages": [
    {
      "root": "pages/sub",
      "pages": [
        {
          "path": "sub/index",
          "style": {
            "navigationBarTitleText": "子页面"
          }
        }
      ]
    }
  ],
  "preloadRule": {
    "pages/index/index": {
      "network": "all",
      "packages": ["pages/sub"]
    }
  },
  "plugins": {
    "myPlugin": {
      "version": "1.0.0",
      "provider": "wxidxxxxxxxxxxxxxx"
    }
  },
  "workers": "workers",
  "requiredBackgroundModes": ["audio"],
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "sitemapLocation": "sitemap.json",
  "style": "v2",
  "useExtendedLib": {
    "kbone": true
  },
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "networkTimeout": {
    "request": 60000,
    "connectSocket": 60000,
    "uploadFile": 60000,
    "downloadFile": 60000
  },
  "debug": true,
  "functionalPages": true,
  "resizable": true,
  "navigateToMiniProgramAppIdList": [
    "wxidxxxxxxxxxxxxxx"
  ]
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!