未处理的承诺拒绝 AOT(提前)Angular 2

Unhandled Promise Rejection AOT(Ahead Of Time) Angular 2

我有一个小型 Angular 2 应用程序,它从 ASP.net MVC Web API 获取数据。使用身份验证。我正在尝试在 Angular 2 中使用 Ahead Of Time 编译器。我是 运行 以下命令

node_modules \ .bin\ngc -p src

但我收到以下错误

(node:13640) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Right-hand side of 'instanceof' is not an object (node:13640) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

有人能告诉我怎么解决吗

This is my directory structure

我已将 tsconfig.json 从根目录移动到 src 文件夹,因为它在此 link

中指示

That's how i am using promise

我已经做出了这样的承诺 return 的服务:

public get(servicename: string) {
    this.colorlog.log('get ' + servicename + " ",enmMessageType.Info);
    return this.http.get(this.ngWEBAPISettings.apiServiceBaseUri + "api/" + servicename + "/", this.ngWEBAPISettings.getConfig()).toPromise();
}

并像这样在组件中使用它

get() {
    var promise = this.setupServicePromise.get(this.SERVICE_NAME);

    promise.then((response: any) => {
      this.colorlog.log('in then promise ProductComponent get' + response,   enmMessageType.Data);
      this.vm.Products = response.json();
    }).catch((error: any) => {
      this.colorlog.log("Error Occurred in product", enmMessageType.Error);
    });
}

这是我的 ts.config 文件。

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es2015", "dom"],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

我已按照以下步骤完成此操作

第一

我已经更新了 MrJSingh

提到的版本

更新后package.json依赖部分变成这样

"dependencies": {
  "@angular/common": "~2.4.0",
  "@angular/compiler": "~2.4.0",
  "@angular/compiler-cli": "^2.4.1",
  "@angular/core": "~2.4.0",
  "@angular/forms": "~2.4.0",
  "@angular/http": "~2.4.0",
  "@angular/platform-browser": "~2.4.0",
  "@angular/platform-browser-dynamic": "~2.4.0",
  "@angular/router": "~3.4.0",
  "core-js": "^2.4.1",
  "rxjs": "5.0.1",
  "zone.js": "^0.7.4"
},

第二

之后我更改了 app.module.ts 文件并将 useValue 更改为 useFactory

改变这个

let authService = new AuthService(new LocalStorage());
providers: [
  {provide:AuthService,useValue:authService},
  AdminAuthGuard,
  UserAuthGuard,
  SetupServicePromise,
  SetupServiceObservables
]

至此

export function authServiceFactory() {
  return new AuthService(new LocalStoragee());
}

providers: [
  {provide:AuthService,useFactory:authServiceFactory},
  AdminAuthGuard,
  UserAuthGuard,
  WebAPISettings,
  LoginService,
  SetupServicePromise,
  SetupServiceObservables
]

经过这两个步骤,命令终于运行成功了。

Note

对于最新版本,使用声明而不是提供程序

declarations: [
  {provide:AuthService,useFactory:authServiceFactory},
  AdminAuthGuard,
  UserAuthGuard,
  WebAPISettings,
  LoginService,
  SetupServicePromise,
  SetupServiceObservables
]

有关详细信息,请查看此 link