【发布时间】:2019-10-15 05:13:32
【问题描述】:
我在 catchError 中需要 resolve(false),但这不起作用。
在我的 loin.page.ts
async login() {
if (this.signin.valid) {
const controls = this.signin.controls;
const valid = await this.authService.signin(controls.email.value, controls.password.value);
if (valid) {
this.navCtrl.navigateRoot('/user', { animated: true});
} else {
console.log('Bad credentials');
}
}
}
authService.ts
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map, catchError } from 'rxjs/operators';
constructor(private http: HttpClient) {}
signin(email, password) {
const headers = new HttpHeaders({
Accept: 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
});
const data = {
email,
password
};
const url = URL_SERVICIOS + 'xxx';
return new Promise ((resolve) => {
this.http.post(url, data).pipe(
map( (result: any) => {
this.credentials = result;
resolve(true);
}),
catchError(() => {
return Promise.resolve(false);
})
).subscribe();
});
}
此代码以 'return Promise.resolve(false);' 结尾,并且不会在 loin.page.ts
中继续【问题讨论】:
-
而不是
pipe,你不想要then吗?
标签: typescript promise async-await ionic4 resolve