node.js - 未处理的 promise 拒绝 AOT(提前)Angular 2

标签 node.js asp.net-mvc angular angular2-aot

我有一个小型 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

我已按照此 link 中的指示将 tsconfig.json 从根目录移动到 src 文件夹

Directory structure

That's how i am using promise

我已经做出了这样的 promise :

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

关于node.js - 未处理的 promise 拒绝 AOT(提前)Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386834/

相关文章:

javascript - 如何在 ASP.NET MVC 中为单选按钮列表分配默认值?

c# - Foreach 与来自 viewbag 的数据

javascript - 为 Angular 2 中的路由通用组件提供异步依赖关系。路由的解析等效项

Angular :ReferenceError: 'function' 未定义

javascript - 从 Node 传递的变量不会显示在 EJS 文件中

javascript - Node、Webpack-Dev-Server、React 的开发模式 - 嵌套路由没有此类文件或目录

javascript - 将数据从 Jquery 发送到 Controller 。 MVC C#

angular - 如何使用 ngClass 函数中的变量?

node.js - 从 mongodb 游标流式传输到 node.js 中的 Express 响应

javascript - 适用于多种环境的 Grunt 任务自动化