【问题标题】:How to pass Async data return from an Observable from Child to Parent component in Angular如何将来自 Observable 的异步数据返回从子组件传递到 Angular 中的父组件
【发布时间】:2021-02-01 21:43:37
【问题描述】:

我正在尝试将异步数据从子组件传递到父组件。但是在这里console.log(this.values) 里面ngAfterViewInit() 在父组件html 页面加载中给出了一个空值。但是,如果我单击子组件中的应用按钮,它将在getValues($event) 方法打印内发出console.log(this.values); 事件。

这是我的父组件 .ts 文件

name = 'ViewUI';
@ViewChild('ChildValues', { static: false }) childReference: ChildComponent;

 ngAfterViewInit() {
  this.values = this.childReference.this.responseValues;
  console.log(this.values);
  mapResponse(this.values);
}

In here `mapResponse(this.values)` method runs before coming data from `this.values` from child component to parent.
getValues($event) {
  this.values = $event;
  console.log(this.values);
  this.apply();
}

这是我的父组件 .html 文件

<app-child #ChildValues (getEvent)=" getValues($event)" [name]="name"></app-child>

这是我的子组件 .html 文件。

<button (click)="applyResponses()" mat-raised-button>
  Apply
</button>

这是我的子组件 .ts 文件

  export class ChildComponent implements OnInit {
      responseValues = {
            names: [], ages: []
        };
      @Output() getEvent = new EventEmitter < any > ();
      @Input() name: string;
    
      ngOnInit() {
        this.getNames();
        this.getAges();
        console.log(this.name);
      }
    
     getNames() {
       this.apiService.getnames(Id).subscribe((names) => {
         this.responseValues.names = [names[0].name];
        });
     }
    
     getAges() {
       this.apiService.getages(Id).subscribe((ages) => {
         this.responseValues.ages = [ages[0].age];
        });
     }
      applyResponses() {
        this.getEvent.emit(this.responseValues);
        console.log(this.responseValues);
      }
    }

如何在父页面加载时从子组件获取数据到父组件?

【问题讨论】:

  • 为什么要将数据从子组件传递给父组件?为什么不反过来呢?它将简化您的用例。
  • 是的,我有父组件,并且有一个子组件可以下拉栏

标签: angular typescript angular-components viewchild asyncdata


【解决方案1】:

为什么不在 child 的 ngOnInit 中定义一个新的发射器?

@Output()dataReceived= new EventEmitter < any > ();

ngOnInit()
{
   forkJoin(this.getNames(),this.getAges()).subscribe(([names,ages]:[any[],any[]])=>{
       this.dataReceived.emit({names:names,ages:ages})
   })
}

你的父母呢?

<app-child (dataReceived)="storeData($event)"></app-child>
storeData(data:any)
{
    console.log(data.names,data.ages)
}

更新这是对评论中问题的回答,请记住,您不确定是否获得了数据。为了检查它,我们将“模拟”来自服务器的缓慢响应。对于来自 'rxjs/operators' 的导入延迟

import {delay} from 'rxjs/operators'

并通过以下方式更改订阅

this.apiService.getages(Id).pipe(
  delay(10000))
  .subscribe((ages) => {
     this.responseValues.ages = [ages[0].age];
    });

如果你在 10 秒前点击按钮,你没有数据

【讨论】:

  • 但是如果我检查 console.log(this.values); 里面的 ngAfterViewInit() 它仍然显示一个空对象
  • Mandara,你不知道数据什么时候准备好——你正在处理异步调用——所以你需要“做点什么”在订阅函数中(假设你调用了花费10 秒,你在这 10 秒之前点击按钮——你也没有价值——),
  • 其实如果点击应用按钮数据来了。但是我想获取父组件初始页面加载的数据html (click)="applyResponses()
  • 我想运行像mapResponse() 这样的另一个函数,我想将那些this.values 发送到另一个mapResponse() 方法。但是这个方法mapResponse() 在this.values 之前调用来自子组件
  • Mandara,如果您单击应用按钮,数据即将到来,因为您的通话已完成(通常通话最多可以花费 1 或 2 秒),但您不知道数据何时准备就绪。你确定在订阅函数中你得到了数据,但没有更多。 (我将答案更新为“模拟延迟)
猜你喜欢
  • 2019-01-13
  • 2020-04-05
  • 2017-06-25
  • 2020-07-31
  • 1970-01-01
  • 2017-12-24
  • 2019-04-24
  • 2022-01-19
  • 2018-11-30
相关资源
最近更新 更多