好的,
我有一个 Angular 8 应用程序和一个 HttpMaintenanceInterceptor,但我没有使用 cookie。但是:
authService 中的 getAccessToken 方法,如下所示:
getAccessToken(): string {
return this._user ? this._user.access_token : null;
}
我有一个 get 方法。 get 方法如下所示:
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
但现在的问题是属性:
patientUUID
始终为空。或者这是它的输出:
http://localhost:4200/api/patient/$%7BpatientUUID%7D/DossierEntry/type/physical"
所以我尝试在 HttpMaintenanceInterceptor 中发送病人UUID。
HttpMaintenanceInterceptor 看起来像这样:
export class HttpMaintenanceInterceptor implements HttpInterceptor {
needsAuthenticatedUser = true;
route: string;
constructor(private auth: AuthService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const accessToken = this.auth.getAccessToken();
if (accessToken != null) {
console.log(accessToken);
const duplicate = request.clone({
setHeaders: { Authorization: `Bearer ${accessToken}` }
});
const user$ = this.auth.loginStatus()
.pipe( take( 1 ) );
user$.pipe(
map( user => {
console.log('hello there nice to see you!!');
let parsedRoute = this.route;
if ( this.needsAuthenticatedUser ) {
if ( !user ) {
throw Error( 'Tried to call api that requires login without a user profile present' );
} else {
parsedRoute = parsedRoute.replace('{userId}', user.profile.sub);
// console.log('User Sub ' + user.profile.sub);
console.log('User participant ' + user.profile.participant);
parsedRoute = parsedRoute.replace('{patientUUID}', user.profile.participant);
}
}
return environment.ApiOrigin + parsedRoute;
} ),
);
return next.handle(duplicate);
} else {
return next.handle(request);
}
}
}
但我没有得到病人UUID。
但是我得到了accessToken:console.log(accessToken);看起来像这样:
mIwM2U2MGNhMzgwYzczMzA2NjIcHM6Ly9k.....
所以我的问题是如何传递病人UUID?这样在 api 请求中,病人 UUID 将不再为空。
谢谢
好吧,我改成这样:
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>(`/api/patient/{patientUUID}/DossierEntry/` + entryType);
}
但这不是我认为的问题。
因为问题是这样的:
console.log('你好,很高兴见到你!!');
它没有达到那条线。
最佳答案
应该使用反引号而不是简单的引号
'/api/patient/${patientUUID}/DossierEntry/'
应该是
`/api/patient/${patientUUID}/DossierEntry/`
使用parsedRoute.replace
时也有同样的情况
const user$ = this.auth.loginStatus()
.pipe( take( 1 ) );
user$.pipe(
map( user => {
console.log('hello there nice to see you!!');
let parsedRoute = this.route;
if ( this.needsAuthenticatedUser ) {
if ( !user ) {
throw Error( 'Tried to call api that requires login without a user profile present' );
} else {
parsedRoute = parsedRoute.replace('{userId}', user.profile.sub);
// console.log('User Sub ' + user.profile.sub);
console.log('User participant ' + user.profile.participant);
parsedRoute = parsedRoute.replace('{patientUUID}', user.profile.participant);
}
}
return environment.ApiOrigin + parsedRoute;
} ),
);
这部分代码永远不会被执行,因为您没有订阅可观察的内容。这就是为什么 console.log
的值永远不会打印到控制台
关于angular - 如何在拦截器Angular 8中传递用户ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079855/