您希望首先获取AuthToken,然后进行第二次调用。要做到这一点,您可能需要
map
然后返回实际值
Observable
从里面。但既然这能给你一个
Observable<Observable<PlatformInterface[]>>
flatMap
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { NbAuthService, NbAuthJWTToken } from '@nebular/auth';
import { flatMap } from 'rxjs/operators';
interface PlatformInterface {
id: number;
name: string;
}
@Injectable()
export class PlatformService {
data = [];
constructor(
private authService: NbAuthService,
private http: HttpClient
) {}
getData() {
return this.authService.getToken()
.pipe(flatMap(token => {
return this.http.get <PlatformInterface[]>(
'http://app-backend.test/app/platforms', {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
})
});
}));
}
}