【问题标题】:Javascript syntax error using Cordova in Android 6在 Android 6 中使用 Cordova 的 Javascript 语法错误
【发布时间】:2020-02-19 13:46:54
【问题描述】:

我正在使用 Cordova,但在 Android API 23 (6 Marshmallow) 中有语法错误。
在 API 28(9 Pie)和 API 21(5 Lollipop)上一切正常,但在 API 23 上,我有这些错误:

Uncaught SyntaxError: Unexpected token =>
Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
Uncaught SyntaxError: missing ) after argument list

我使用的是原版 JS,没有别的。

有什么问题?我该如何解决?

【问题讨论】:

  • 你使用的node版本是什么?
  • @Prawinsoni 我正在使用 v12.13.0
  • 好的,问题出在节点 5 上。您尝试使用严格模式了吗?
  • @Prawinsoni 显然问题是 ES6 在 API 23 中的不兼容,“严格模式”也是一个问题。我找到了解决方案,但它可能不适用于旧手机。

标签: javascript cordova android-6.0-marshmallow


【解决方案1】:

问题似乎来自 API 23 中的 ES6 不兼容。
为了解决这个问题,我使用babel

sudo npm install --save-dev @babel/preset-env

创建一个 .babelrc 文件

{
    "presets": ["@babel/preset-env"],
    "sourceType": "script" //To remove 'use strict' of generated js files
}

然后你就可以用这个命令把你的代码转成es5了
npx babel src/js -d www/js

可以使用钩子自动执行此命令。 只需创建一个 .sh 文件(例如 build.sh)并将命令添加到其中。 将钩子添加到 config.xml 文件
<hook type="before_build" src="hooks/build.sh" />

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多