我構建了一個服務,它從API可觀察數據獲取特定的ID。該服務正在工作,如果我從服務類的console.log(數據),但我無法獲取我的組件中的數據。從API服務角度獲取數據4
服務:
getSpecificStory(storyId) {
return this.getToken()
.map(idToken => {
let headers = new Headers();
headers.set('user_token', idToken)
return this.http
.get(`${this.apiUrl}/stories/${storyId}`, { headers: headers })
.subscribe((res: Response) => {
const data = res.json();
console.log(data)
return data;
});
})
.catch(this.handleError);
}
組件:
export class StoryModalComponent implements OnInit {
story: any;
storyId: any;
hitsArray: Array<Object>;
constructor(private storiesService: StoriesService, private route: ActivatedRoute) {
}
ngOnInit() {
this.route.params
.subscribe(
params => {
this.storyId = params['storyId']
})
console.log(this.storyId)
this.getStoryObject();
}
getStoryObject() {
console.log(this.storyId)
this.storiesService.getSpecificStory(this.storyId)
.subscribe(
(data) => {
this.story = data;
console.log(this.story)
})
}
}
如何做你的組件的外觀? – Sajeetharan
就像我發佈的?你的意思是HTML?順便說一下,不要介意「getToken()」函數 - 它只是用於在Firebase後端獲取用戶的id_token。 – byblix