gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:30:03 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com