HttpInterceptor在Angular 6中不起作用

时间:2018-08-22 13:39:49

标签: angular command-line-interface angular-http-interceptors

我正在使用HttpInterceptor添加Auth令牌,问题是拦截器正在为第一个请求添加令牌,然后它停止工作。这是我的拦截器服务

import { Injectable } from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Observable} from "rxjs/internal/Observable";
import {CommonService} from "./common.service";

@Injectable({
  providedIn: 'root'
})
export class AuthInterceptorService implements HttpInterceptor {

  constructor(private auth: CommonService) { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const authToken = this.auth.getAuthToken();
    const authReq = req.clone({ setHeaders: { Authorization: 'Bearer '+authToken.access_token} });
    return next.handle(authReq);
  }
}

这是“应用模块”提供商部分

providers: [{
    provide: HTTP_INTERCEPTORS,
    useClass: AuthInterceptorService,
    multi: true
  }],

让我知道代码有什么问题。

0 个答案:

没有答案