gpt4 book ai didi

javascript - 在 Angular 4 中使用等待时生成器未定义错误

转载 作者:行者123 更新时间:2023-11-28 03:51:23 25 4
gpt4 key购买 nike

我有一个使用 Angular 4.4.6 版本的项目。 this is version of angular

当我配置它并运行时,就可以了。但是在函数 onSubmit Login 中使用 async 和 wait 时出现问题:login.component.ts:

public async onSubmit() {
this.submitted = true;
if (this.form.valid) {
const user: ILoginUser = {
UserName: this.form.value.username,
Password: this.form.value.password
};
this.isLoginSuccessful = await this._loginService.login(user, true);
}
}

不知道为什么抛出异常exception here 。我认为 tsconfig.json 中有问题,但我不知道我错了的确切位置。

这是 tsconfig.json:

{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noEmit": true,
"noEmitHelpers": false,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
],
"plugins": [
{
"name": "tslint-language-service"
}
]
},
"exclude": [
"node_modules",
"dist"
],
"awesomeTypescriptLoaderOptions": {
"forkChecker": true,
"useWebpackText": true
},
"buildOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}

Everyone please help me, many thanks!

最佳答案

我建议使用订阅而不是使用异步和等待。如果您遇到任何错误,请告诉我。

await works for promise, while your login function must be returning an observable, so you might want to convert the observable to promise, if you must use await like:
>> await .login(,,).toPromise()

public onSubmit() {
this.submitted = true;
if (this.form.valid) {
const user: ILoginUser = {
UserName: this.form.value.username,
Password: this.form.value.password
};
this._loginService.login(user, true).subscribe((bool) => {
this.isLoginSuccessful = bool;
});
}
}

关于javascript - 在 Angular 4 中使用等待时生成器未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47975668/

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