Angular ng5中的错误处理
我是angular的新手,我正在使用版本5开发一个小功能。 我有一个register组件,其中我有一个register方法,如下所示,它在按钮的点击事件中被调用Angular ng5中的错误处理,angular,angular5,Angular,Angular5,我是angular的新手,我正在使用版本5开发一个小功能。 我有一个register组件,其中我有一个register方法,如下所示,它在按钮的点击事件中被调用 register() { this.authService.register(this.model).subscribe(m=>{ //console.log("Registeration Successful"); this.alertify.success("Registeration Su
register()
{
this.authService.register(this.model).subscribe(m=>{
//console.log("Registeration Successful");
this.alertify.success("Registeration Successful");
}, error => {
console.log(error);
//console.log("Error while registering.");
this.alertify.error(error);
});
}
在服务中,我有两个方法可以成功地调用API,但我遇到了错误
CatchSubscriber.AuthService.errorHandler
。这可能是由于捕获。我想
从API中获取错误,我需要在用户界面中显示这些错误
import { Injectable } from '@angular/core';
import { RequestOptions, Response } from '@angular/http';
import { HttpClient, HttpHeaders, HttpParams, HttpRequest, HttpDownloadProgressEvent, HttpErrorResponse } from '@angular/common/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Observable';
register(model: any)
{
const httpheaders = new HttpHeaders().set('Content-type','application/json');
return this.httpClient.post(this.baseUrl + 'Register', model, {headers: httpheaders, responseType: 'json' }).catch(this.errorHandler);
}
private errorHandler(error: HttpErrorResponse)
{
console.error(error.message);
return Observable.throw(error.message || 'Server Error');
}
我面临的错误是
TypeError: Observable_1.Observable.throw is not a function
"CatchSubscriber.AuthService.errorHandler"
at CatchSubscriber.error (catchError.js:105)
at MapSubscriber.Subscriber._error (Subscriber.js:134)
at MapSubscriber.Subscriber.error (Subscriber.js:108)
at FilterSubscriber.Subscriber._error (Subscriber.js:134)
at FilterSubscriber.Subscriber.error (Subscriber.js:108)
我也检查了其他,并尝试了相同的方法,但仍然面临此错误使用
catchError
而不是catch
。您的注册方法如下所示:
register(model: any)
{
const httpheaders = new HttpHeaders().set('Content-type','application/json');
return this.httpClient
.post(this.baseUrl + 'Register', model, {headers: httpheaders, responseType: 'json' })
.pipe(catchError(this.errorHandler));
}