【发布时间】:2018-09-18 22:55:02
【问题描述】:
您好我正在尝试通过 http 拦截器添加自定义标头,以下是我的代码
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { StorageService } from 'src/app/services/storage-service';
import { HttpHeaders } from '@angular/common/http';
@Injectable()
export class AuthorizationHeaderInterceptor implements HttpInterceptor {
constructor(private storageService:StorageService ){}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let authToken = this.storageService.getAuthorizationToken();
const authReq = req.clone({
headers: req.headers.set('Authorization', authToken)
});
return next.handle(authReq);
}
}
使用上面的代码我得到'TypeError:无法读取未定义的属性'长度'。但是,随着以下更改,错误消失了,但它没有设置授权标头。
const authReq = req.clone();
authReq.headers.set('authorization',authToken);
代码中可能有什么问题。
提前致谢。
【问题讨论】: