Angular中的HttpErrorInterceptor无法重定向
创始人
2024-10-31 06:00:19
0

在Angular中,HttpErrorInterceptor是用来捕获和处理HTTP请求错误的拦截器。然而,它本身无法直接实现重定向功能。但是,我们可以通过以下方法解决这个问题:

  1. 使用Router来实现重定向:可以在HttpErrorInterceptor中注入Router,并在错误处理函数中使用Router来导航到指定的路由。
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Router } from '@angular/router';

@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
  constructor(private router: Router) {}

  intercept(request: HttpRequest, next: HttpHandler): Observable> {
    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        if (error.status === 401) {
          // Redirect to login page
          this.router.navigate(['/login']);
        }
        return throwError(error);
      })
    );
  }
}
  1. 使用Location来实现重定向:可以在HttpErrorInterceptor中注入Location,并在错误处理函数中使用Location来进行重定向。
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Location } from '@angular/common';

@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
  constructor(private location: Location) {}

  intercept(request: HttpRequest, next: HttpHandler): Observable> {
    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        if (error.status === 401) {
          // Redirect to login page
          this.location.go('/login');
        }
        return throwError(error);
      })
    );
  }
}

请注意,以上示例中的重定向路径('/login')仅作为示例,你需要根据你的实际情况进行修改。同时,确保在app.module.ts中将HttpErrorInterceptor添加到providers数组中以进行拦截器的注册。

import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpErrorInterceptor } from './http-error.interceptor';

@NgModule({
  imports: [
    HttpClientModule
  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpErrorInterceptor,
      multi: true
    }
  ]
})
export class AppModule { }

这样,当HTTP请求返回401错误时,用户将被重定向到指定的路由。

相关内容

热门资讯

第8分钟了解!闲逸手机麻将有挂... 第8分钟了解!闲逸手机麻将有挂的么(辅助挂)起初是有挂(专业辅助高科技教程)-哔哩哔哩;闲逸手机麻将...
第3分钟了解!大同麻将有挂(辅... 第3分钟了解!大同麻将有挂(辅助挂)总是有挂(专业辅助揭秘教程)-哔哩哔哩;亲,有的,ai轻松简单,...
七分钟了解!大宝苏北麻将挂(辅... 七分钟了解!大宝苏北麻将挂(辅助挂)素来是有挂(专业辅助介绍教程)-哔哩哔哩;大宝苏北麻将挂是一种具...
6分钟了解!财神十三张牌辅助器... 6分钟了解!财神十三张牌辅助器下载(辅助挂)从来真的有挂(专业辅助系统教程)-哔哩哔哩;是一款可以让...
八分钟了解!约战丹东麻将可以赢... 八分钟了解!约战丹东麻将可以赢(辅助挂)竟然有挂(专业辅助插件教程)-哔哩哔哩;1、很好的约战丹东麻...
1分钟了解!开心十三张辅助io... 您好:开心十三张辅助ios版这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
第五分钟了解!指尖辅助软件(辅... 第五分钟了解!指尖辅助软件(辅助挂)固有是有挂(专业辅助2025新版技巧)-哔哩哔哩;1、这是跨平台...
5分钟了解!闲玩暗宝外挂(辅助... 您好:闲玩暗宝外挂这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
6分钟了解!乐山幺鸡麻将辅牌器... 6分钟了解!乐山幺鸡麻将辅牌器购买(辅助挂)原生存在有挂(专业辅助线上教程)-哔哩哔哩;乐山幺鸡麻将...
十分钟了解!jj斗地主控牌(辅... 十分钟了解!jj斗地主控牌(辅助挂)都是存在有挂(专业辅助详细教程)-哔哩哔哩;小薇(透视辅助)致您...