在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  網(wǎng)絡(luò)安全/ angular5中HttpClient攔截器catchError調(diào)用函數(shù)的thi

angular5中HttpClient攔截器catchError調(diào)用函數(shù)的this指向誰?

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const token = AuthStorage.getItem('mano');
    const token_snap = AuthStorage.getItem('mano_snap');
    req = req.clone({
      setHeaders: {
        'APIKEY': '33726880C9CE84E67A5C27BD4A2CE91AD2',
        'token': token ? token : token_snap || 'mano'
      }
    });
    return next.handle(req).pipe(
      catchError(this.handleError)
    );
  }
  handleError(error: HttpErrorResponse) {
    console.log(this);    // 此時(shí)的this指向
    const code = error['status'];
    if (code === 401 || code === 417) {
      Auth.loginOut();
    }
    const err = {url: error['url'], status: error['status'], msg: error['error'].msg};
    return new ErrorObservable(err);
  }

在handleError方法中怎么調(diào)用其它方法呢?

回答
編輯回答
好難瘦

你在intercept最后不是調(diào)用了this.handleError么,這個(gè)this和handleError方法里面的this就是同一個(gè)吧,都是指向當(dāng)前class對象。你在class里面定義的方法都可以直接用this調(diào)用。
或者注入服務(wù)以后通過this.xxxService.xxx()調(diào)用

2017年1月12日 19:48
編輯回答
心癌

你這么寫是指向調(diào)用者,如果要綁定this,請使用arrow function或者bind。

2018年2月6日 02:53
編輯回答
不將就

指向調(diào)用者,非此 class。
如果要指向此 class ,請使用箭頭函數(shù)。

    return next.handle(req).pipe(
      catchError((err) => this.handleError(err))
    );
2017年12月16日 09:56