angular4 使用HttpClient拦截器 检查token失效,返回登录页面

1、首先创建一个拦截器服务:InterceptorService.ts
2、在app.module.ts文件里引入拦截器
import {InterceptorService} from ‘./InterceptorService’;
import {HTTP_INTERCEPTORS, HttpClientModule} from ‘@angular/common/http’;
接着在提供器里声明服务
providers: [
{provide: NZ_I18N, useValue: zh_CN},
{provide: HTTP_INTERCEPTORS, useClass: InterceptorService, multi: true}
],
3、InterceptorService.ts:
import {Injectable} from ‘@angular/core’;
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse} from ‘@angular/common/http’;
import {Observable} from ‘rxjs/internal/observable’;
import {mergeMap} from ‘rxjs/internal/operators/mergeMap’;
import {Router} from “@angular/router”;
import {NzMessageService} from “ng-zorro-antd”;

@Injectable()
export class InterceptorService implements HttpInterceptor {
constructor(private router: Router, private message: NzMessageService) {
}
intercept(req: HttpRequest, next: HttpHandler): Observable

猜你喜欢

转载自blog.csdn.net/wsqwsq1596/article/details/82706564
今日推荐