Angular 如何使用ToastrService显示服务器错误的Toastr通知
我正在使用ToastrService显示服务器错误的通知。但这似乎不是工作。在Ui中,如果get请求由于备份问题未将数据加载到表格视图,如何在特定Ui中显示Toast消息,组件?我是否需要在该paricular组件或Angular 如何使用ToastrService显示服务器错误的Toastr通知,angular,angular-http-interceptors,server-error,angular-toastr,angular-errorhandler,Angular,Angular Http Interceptors,Server Error,Angular Toastr,Angular Errorhandler,我正在使用ToastrService显示服务器错误的通知。但这似乎不是工作。在Ui中,如果get请求由于备份问题未将数据加载到表格视图,如何在特定Ui中显示Toast消息,组件?我是否需要在该paricular组件或app.component.ts文件中添加超时之类的内容。 这是我的httpInterceptor类 @Injectable({ providedIn: 'root' }) export class InterceptorService implements HttpInterc
app.component.ts文件中添加超时之类的内容。
这是我的httpInterceptor类
@Injectable({
providedIn: 'root'
})
export class InterceptorService implements HttpInterceptor {
constructor(public toasterService: ToastrService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap(evt => {
if (evt instanceof HttpResponse) {
if (evt.body && evt.body.success) {
this.toasterService.success(evt.body.success.message, evt.body.success.title, {positionClass: 'toast-bottom-center'});
}
}
}),
catchError((err: any) => {
if (err instanceof HttpErrorResponse) {
try {
this.toasterService.error(err.error.message, err.error.title, {positionClass: 'toast-bottom-center'});
} catch (e) {
this.toasterService.error('An error occurred', '', {positionClass: 'toast-bottom-center'});
}
//log error
}
return of(err);
}));
}
}
这是我的messageService类
export class MessageService {
constructor(private toastr: ToastrService) { }
showSuccessWithTimeout(message, title, timespan) {
this.toastr.success(message, title , {
timeOut : timespan
});
}
showError(message, title) {
this.toastr.error(message, title, {
enableHtml : true
});
}
}
当请求超时或找不到后端服务器url或出现类似的服务器错误时,如何向用户显示Toast消息?是否为ngx toastr添加了必要的CSS导入?我添加了
npm install ngx toastr--save
&npm install@angulation/animations--save
export class MessageService {
constructor(private toastr: ToastrService) { }
showSuccessWithTimeout(message, title, timespan) {
this.toastr.success(message, title , {
timeOut : timespan
});
}
showError(message, title) {
this.toastr.error(message, title, {
enableHtml : true
});
}
}