我没有收到任何错误,并且能够使用有效令牌(我已通过 Postman 验证)从我的登录组件设置 authToken 值。但是我没有将任何授权参数添加到我的请求标头中。
entryComponents: [
ConfirmDialogTargetComponent
],
providers: [
AuthInterceptor,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class AuthInterceptor {
authToken: string = '';
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authReq = req.clone({ headers: req.headers.set('Authorization', 'Bearer ' + this.authToken) });
return next.handle(authReq);
}
}