Ionic - Uncaught SyntaxError: Unexpected token On APK

Ionic - Uncaught SyntaxError: Unexpected token On APK

我从昨天开始尝试修复错误。我使用以下方法成功构建了一个 android apk:

ionic cordova build android --prod --release

但该应用在设备和模拟器 (GenyMotion) 上卡在白屏上。我检查了 GenyMotion 的 logcat。我想出了这些:

>  I/chromium( 5497): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token )", source: http://localhost/runtime.fcad956485f9614ff8b1.js (1)
>  D/SystemWebChromeClient( 5497): http://localhost/polyfills.49d987a1d15d60566c3f.js: Line 1 : Uncaught  SyntaxError: Unexpected token =>
> I/chromium( 5497): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token =>", source: http://localhost/polyfills.49d987a1d15d60566c3f.js (1)
> D/SystemWebChromeClient( 5497): http://localhost/main.2be168d95557cbdc0a3f.js: Line 1 : Uncaught SyntaxError: Unexpected token =>

> I/chromium( 5497): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token =>", source: http://localhost/main.2be168d95557cbdc0a3f.js (1)

我不知道为什么。构建时没有错误。

我的package.json

... 
"@angular/common": "~12.0.1",
"@angular/core": "~12.0.1",
"@angular/forms": "~12.0.1",
"@angular/platform-browser": "~12.0.1",
"@angular/platform-browser-dynamic": "~12.0.1",
"@ionic/angular": "^5.5.2",
"ionic": "^5.4.16",
"cordova-android": "^9.0.0",
"cordova-browser": "^6.0.0",
"cordova-ios": "^5.1.1",
"cordova-plugin-ionic-webview": "^5.0.0",
... 
"@typescript-eslint/eslint-plugin": "4.16.1",
"@typescript-eslint/parser": "4.16.1",
"typescript": "~4.2.4"

我的tsconfig.json.

...  
"target": "es2015", 
"module": "es2020",
"lib": ["es2018", "dom"] 
...  

这里已经很清楚了。你有一个语法错误。我想您可以检查 ionic cordova build android --prod 的日志。可能它会显示哪些文件和行产生了这些问题。

如果您使用 linter,您可以尝试 运行 npm run lint 检查您的代码是否存在任何问题。

为了解决这个问题,经过3天的实战, 我解决了这个问题。 Ionic 5 angular 12 在构建时使用 es2015 作为目标。 我在 tsconfig.json 中将“目标”设置为 es5,在运行时出现错误。**.js,polyfills。**。json 消失了

es5 将 ES6 函数转换为标准函数。

更多信息,请访问 https://www.typescriptlang.org/tsconfig