简体   繁体   English

子组件在初始化之前要等待父组件ngOnInit异步操作

[英]Child Component to wait for Parent Component ngOnInit async operation before initializing

I'm using mat-stepper for implementing customer onboarding process. 我正在使用mat-stepper来实现客户的入职流程。 All 5 individual child mat-stepper component are inside one parent component. 所有5个单独的child mat-stepper component都位于一个parent部件内。

<parent-html>
   <mat-horizontal-stepper #ccStepper [linear]="isLinear">
     <mat-step [stepControl]="childAForm">
       <child-a></child-a>
     </mat-step>
   </mat-horizontal-stepper>

 <mat-horizontal-stepper #ccStepper [linear]="isLinear">
     <mat-step [stepControl]="childBForm">
       <child-b></child-b>
     </mat-step>
   </mat-horizontal-stepper>
//3 more child components
</parent-html>

I have to save and proceed data for each child component. 我必须save and proceed处理每个子组件的数据。 applicationID is generated after save and proceed of first child component and my second , third and fourth child components have to persist data entered in first component based on applicationID . first child组件save and proceed之后save and proceed生成applicationID ,我的secondthirdfourth个子组件必须保留基于applicationIDfirst组件中输入的数据。

To further complicate things, whenever user saves information, it has to be stored as draft with applicationID generated (different route altogether). 为了进一步使事情复杂化,每当用户保存信息时,都必须将其存储为draft并生成applicationID (完全不同的路由)。 User can then click on applicationID and all the same information have to be fetched and user can then edit the information of previous and next component. 然后,用户可以单击applicationID并且必须提取所有相同的信息,然后用户可以编辑上一个和下一个组件的信息。

For normal save and proceed, I am storing applicationID in behaviour subject and fetching all the information in all child components. 为了正常保存并继续,我将applicationID存储在behaviour subject并在所有子组件中获取所有信息。

Is it the right way to do as it triggers multiple calls even though I have information available in sibling components? 即使我在同级组件中有可用信息,它也会触发多个调用,这是正确的方法吗?

For edit scenario, I am passing the customer information via @Input to all child components. 对于编辑方案,我通过@Input将客户信息@Input给所有子组件。 The problem here is child ngOnInit is fired before parent ngOnInit has resolved the @Input value. 这里的问题是在parent ngOnInit解析@Input值之前会触发child ngOnInit hence always failing the condition check. 因此总是无法通过条件检查。

What would be the best approach to solve it? 解决该问题的最佳方法是什么? Thanks for your time!!! 谢谢你的时间!!!

For Edit Scenario, If you pass value using @Input decorator from Parent to Child Component. 对于“编辑方案”,如果使用@Input装饰器将值从父级传递到子级组件。 You can access those values using ngOnChanges() method in child component. 您可以使用子组件中的ngOnChanges()方法访问这些值。

Child Component: 子组件:

ngOnChanges(changes: SimpleChanges) {
    if(this.inputData){}
}

For Application ID, you can set the application ID in a singleton service and you can access that application ID in child component by injecting the service. 对于应用程序ID,可以在单例服务中设置应用程序ID,并且可以通过注入服务在子组件中访问该应用程序ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM