我正在使用ngrx/data,为了设置存储,我使用解析器。
用户解析器.ts
@Injectable()
export class UserResolver implements Resolve<any> {
loading = false;
constructor(private userEntityService: UserEntityService) {}
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any> {
return this.userEntityService.loaded$.pipe(
tap(loaded => {
if (!loaded) {
this.userEntityService.getAll();
}
}),
filter(loaded => !!loaded),
first()
);
}
}
用户路由.ts
path: '',
component: UsersComponent,
resolve: {
user: UserResolver
},
问题是当我使用解析器时,我的 Auth 拦截器没有将令牌附加到标头。当我评论“解决”时,一切正常。所有请求均已获得授权。
auth.interceptor.ts
const userProfileStr = localStorage.getItem('userProfile');
if (userProfileStr) {
const userProfile: User = JSON.parse(userProfileStr);
const jwtToken = userProfile.jwtToken;
const csrf = userProfile.csrf;
let headers = new HttpHeaders();
headers = req.headers
.set('Authorization', jwtToken)
.set('x-csrf-token', csrf)
.set('Content-Type', 'application/json');
const cloneReq = req.clone({ headers });
return next.handle(cloneReq);
} else {
return next.handle(req);
}
可能是什么原因?