无法使用 AOT 运行 {N} 应用程序,Reflect.getMetadata 不是函数

Unable to run {N} application with AOT, Reflect.getMetadata is not a function

尝试使我的 nativescript 应用程序 运行 启用 aot。 使用最新版本的 nativescript,它构建得很好。启动应用程序时出现错误 Reflect.getMetadata is not a function,应用程序无法运行。 当使用依赖于 Reflect.getMetadata 的依赖项时会发生这种情况,例如:https://github.com/ghidoz/angular2-jsonapi

使用aot时出错的Demo项目: https://github.com/annervisser/nativescript-aot-error

应用程序 运行 完美地使用了 JIT,只有在使用 --env.aot 标志时才会中断。 Webpack 始终启用,因为这是一个 angular 项目(始终 运行 带有 --bundle 标志)

完整堆栈跟踪:

System.err: TypeError: Reflect.getMetadata is not a function
System.err: File: "<unknown>, line: 1, column: 265
System.err: 
System.err: StackTrace: 
System.err:     Frame: function:'saveAnnotations', file:'file:///data/data/nl.persistence.stubs/files/app/vendor.js', line: 78279, column: 36
System.err:     Frame: function:'', file:'file:///data/data/nl.persistence.stubs/files/app/vendor.js', line: 78322, column: 13
System.err:     Frame: function:'module.exports../app/shared/models/user.model.ts.__decorate', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 2287, column: 110
System.err:     Frame: function:'', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 2302, column: 5
System.err:     Frame: function:'./app/shared/models/user.model.ts', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 2348, column: 2
System.err:     Frame: function:'__webpack_require__', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 752, column: 30
System.err:     Frame: function:'fn', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 122, column: 20
System.err:     Frame: function:'./app/shared/auth/auth.service.ts', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 1355, column: 76
System.err:     Frame: function:'__webpack_require__', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 752, column: 30
System.err:     Frame: function:'fn', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 122, column: 20
System.err:     Frame: function:'./app/shared/auth/auth-interceptor.service.ts', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 1280, column: 71
System.err:     Frame: function:'__webpack_require__', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 752, column: 30
System.err:     Frame: function:'fn', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 122, column: 20
System.err:     Frame: function:'./app/app.module.ngfactory.js', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 1041, column: 96
System.err:     Frame: function:'__webpack_require__', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 752, column: 30
System.err:     Frame: function:'fn', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 122, column: 20
System.err:     Frame: function:'', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 2387, column: 83
System.err:     Frame: function:'./main.ts', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 2465, column: 30
System.err:     Frame: function:'__webpack_require__', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 752, column: 30
System.err:     Frame: function:'checkDeferredModules', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 45, column: 23
System.err:     Frame: function:'', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 828, column: 18
System.err:     Frame: function:'', file:'file:///data/data/nl.persistence.stubs/files/app/bundle.js', line: 831, column: 10
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:     Frame: function:'', file:'file:///data/data/nl.persistence.stubs/files/app/starter.js', line: 2, column: 1
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err: 
System.err:     at com.tns.Runtime.runModule(Native Method)
System.err:     at com.tns.Runtime.runModule(Runtime.java:624)
System.err:     at com.tns.Runtime.run(Runtime.java:616)
System.err:     at com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5871)

我试过在不同的地方导入反射元数据,包括polyfills.tsmain.tns.tsapp.module.tns.ts 我也试过将它包含在我的 webpack / tnsconfig 中并将目标更改为 es2015 或 es6。 None 其中有任何区别。

搜索此错误会得到 angular 的大量一般信息,建议将 reflect-metadata 的导入添加到 polyfills.ts。这没有用。

在您的应用模块顶部添加 reflect-metadata 包的导入语句(基本上在访问 JsonApiDatastoreConfig 装饰器之前。

import "reflect-metadata";