当我尝试使中间件进行身份验证时发生错误

时间:2019-08-07 07:47:49

标签: angular typescript angular7

我尝试将jwt Web令牌挂接到我的传出请求,显示添加了拦截器“ ERROR TypeError:无法读取未定义的属性'length'”后,我使用了拦截器方法。 当拦截器文件被删除时,应用程序正常工作  This is my dev console

这是我的拦截类

interceptor

1 个答案:

答案 0 :(得分:0)

这是一个简化的版本,但在我的应用程序中它就像一个魅力:

public intercept(
            req: HttpRequest<any>,
            next: HttpHandler
        ): Observable<HttpEvent<any>> {

        req = this.addAuthHeader(req);
    }

    addAuthHeader(request) {
         const r: HttpRequest<any> = request.clone({
             headers: new HttpHeaders({
                    Authorization: 'Bearer ' + this.authService.getToken()
             })
         });
         return r;
    }

供参考-> how-to-add-multiple-headers-in-angular-5-httpinterceptorinterceptor-angular-4-3-set-multiple-headers-on-the-cloned-request

致谢