[英]Pass api response from one component to another component in Angular
[英]How to pass value one component to another component after getting response from an API in Angular 7
我正在處理一個 API 問題。 在收到來自 API 的響應后,我想將該值傳遞給子組件。 在那里我將執行方法並獲得響應,並將其顯示在屏幕上。
代碼:
ngOnInit() {
const userId = '2';
this.apiService.collectIds(userId)
.subscribe(ids => {
this.ids = ids
}, error => {
console.log('error', error);
});
}
子組件:
<app-chart [ids]="ids"></app-chart>
你有沒有嘗試在視圖中放置異步?
<app-chart [ids]="ids | async"></app-chart>
試試這個
ngOnInit() {
const userId = '2';
this.collectIds$ = this.apiService.collectIds(userId);
}
在 html 中:
<app-chart [ids]="(collectIds$|async)?.ids"></app-chart>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.