Angular 7拦截器未在请求中添加标头

时间:2019-04-20 14:40:00

标签: angular7

我正在使用angular 7,这是我的拦截器代码

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler,HttpHeaders, HttpRequest, HttpEvent } from '@angular/common/http';
import { TokenStorageService } from '../services/token-storage.service';
import {Router} from '@angular/router';
const TOKEN_HEADER_KEY = 'Authorization';
import {observable, Observable} from 'rxjs';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
    constructor(private token: TokenStorageService,private route:Router) { }

    intercept(req: HttpRequest<any>, next: HttpHandler):Observable<HttpEvent<any>> {
        let authReq = req;
        const token = this.token.getToken();
        if (token != null) {
            authReq = req.clone({headers:new HttpHeaders({
                Authorization: 'Bearer '+token,
                "Content-Type": "application/json"
              })});
        }

        return next.handle(authReq);
    }
}

我尝试了setHeaders以及互联网上提到的所有其他可能的情况,但都没有用。在请求的标头部分的 lazyinit lazyupdate 属性中添加了标头。 请指导!

0 个答案:

没有答案