3

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

节点模块\.bin\ngc -p src

但我收到以下错误

(节点:13640)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:2):TypeError:'instanceof'的右侧不是对象(节点:13640)DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

有人能告诉我如何解决吗

这是我的目录结构

我已将 tsconfig.json 从根目录移动到 src 文件夹,正如它在此链接中所指示的那样

目录结构

这就是我使用承诺的方式

我已经提供了服务,我从这些服务中返回了这样的承诺:

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
  }
}
4

1 回答 1

1

我已通过以下步骤完成此操作

第一的

我已经更新了MrJSingh提到的版本

updateh 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
]

最终命令在这两个步骤之后成功运行。

笔记

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

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

有关更多详细信息,请查看此链接

于 2017-01-03T16:05:10.253 回答