简体   繁体   English

使用组件的Angular 2自定义HTTP错误处理

[英]Angular 2 custom HTTP error handling using component

This is what I have so far: 这是我到目前为止的内容:

@Component({
  selector: 'error-handler',
  templateUrl: 'error-handler.html'
})
export class ErrorHandlerComponent extends ErrorHandler {

  text: string;

  constructor() {
    super();
  }

  handleError(error: HttpErrorResponse) {
    console.log("ErrorHandlerComponent, error.status: ", error.status);
    this.text = error.message;
  }
}

But my the flow doesn't go into handleError, I get the below error: 但是我的流程没有进入handleError,我得到以下错误:

polyfills.js:3 Uncaught Response {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers, …}

Your ErrorHandler needs to be an @Injectable , not a @Component . 您的ErrorHandler必须是@Injectable ,而不是@Component It doesn't have a view associated with it - it's a service. 它没有与之关联的视图-这是一项服务。 It should look something like this: 它看起来应该像这样:

@Injectable()
export class MyErrorHandler extends ErrorHandler {

  constructor() {
    super();
  }

  handleError(error) {
    // do whatever

    super.handleError(error);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM