我看到了错误处理的几个解决方案。
其主要思路是这样解释的背后:https://angular.io/api/core/ErrorHandler
我试图实现它赶上类型错误,但它不工作:
@Injectable()
export class ErrorsHandler implements ErrorHandler {
constructor(
private injector: Injector,
) { }
handleError(error: Error | HttpErrorResponse | TypeError) {
const notificationService = this.injector.get(NotificationService);
const dataService = this.injector.get(DataService);
const router = this.injector.get(Router);
if (error instanceof HttpErrorResponse) {
if (!navigator.onLine) {
// No Internet connection
return notificationService.notify('No Internet Connection');
}
// Http Error
// Send the error to the server
console.log(error);
// Show notification to the user
return notificationService.notify(`${error.status} - ${error.message}`);
} else if (error instanceof TypeError) {
console.log('This is TypeError!', error);
}
}
}
在这个ELSEIF块检查
err instanceof Error || err instanceof TypeError