简体   繁体   English

角度拦截器内部的异步问题

[英]Asynchrone issue inside angular interceptor

I have an interceptor that checks if a condition is true it opens a snackbar and wait for the user to click a button ( so subscribe to button observer event ).我有一个拦截器来检查条件是否为真,它会打开一个快餐栏并等待用户点击一个按钮(所以订阅按钮观察者事件)。 If false: it returns the request.如果为假:它返回请求。

intercept(req: HttpRequest<any>,
  next: HttpHandler): Observable<HttpEvent<any>> {
  if (this.rightsService.actualCookies && this.cookiesService.get('cookie') !== this.rightsService.actualCookies) {
    const snackBarRef = this.snackBar.open('Session expired', 'refresh');
    snackBarRef.onAction().subscribe(() => {
      window.location.pathname = window.location.pathname.split('/')[0]
    });
  }
  else {
      return next.handle(req);
  }
}

My issue is that if condition is true the interceptor don't wait for the observable and I think that it returns a void response.我的问题是,如果条件为真,拦截器不会等待可观察的,我认为它返回一个void响应。 thats why other interceptors are getting error :这就是其他拦截器出错的原因:

Cannot read property 'pipe' of undefined

in line排队

intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
   return next.handle(request).pipe(

My question is how to disable interceptor to return a value if (this.rightsService.actualCookies && this.cookiesService.get('cookie') !== this.rightsService.actualCookies) is true我的问题是如果(this.rightsService.actualCookies && this.cookiesService.get('cookie') !== this.rightsService.actualCookies)true如何禁用拦截器返回值

your interceptor MUST return something:你的拦截器必须返回一些东西:

intercept(req: HttpRequest<any>,
  next: HttpHandler): Observable<HttpEvent<any>> {
  if (this.rightsService.actualCookies && this.cookiesService.get('cookie') !== this.rightsService.actualCookies) {
    const snackBarRef = this.snackBar.open('Session expired', 'refresh');
    // angular will subscribe. do whatever you need to do in tap.
    return snackBarRef.onAction().pipe(tap(() => {
      window.location.pathname = window.location.pathname.split('/')[0]
    }));
  }
  else {
      return next.handle(req);
  }
}

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

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