Angular6在app.com中等待ngoninit,然后在儿童触发器中完成ngoninit



在我的项目中,我想在app.com初始化时致电API,并且子组件应等到app.comeent在ngoninit((

中完成

在app.component.ts:

ngOnInit() {
// call getx()
     }

和child.component.ts:

ngOnInit(){
   // call gety()
  }

执行订单:想要gety((等到getx((完成。

也许您可以尝试以下承诺:

在app.component.ts

async ngOnInit(): Promise<bool> {
return new Promise<bool>(resolve,reject)=>{
  // call getx()
resolve(true); //or anything you want ot return maybe from getx()
});
}

在child.component.ts .. extends您的app.com.ponent

async ngOnInit(): Promise<bool>{
   await super.NgOnInit();
 // call gety()
   resolve(true); //or anything you want ot return maybe from gety()
});
}

希望它能为您提供帮助。也许您也可以通过其他方式进行存档(例如,从您的App.component.ts中执行发射器,并在您的孩子组成部分中收听它..或其他(..希望它能为您提供帮助!!!

在您的应用程序组件中您可以拥有一个属性:

wasGetXExecuted = false;

执行GetX((函数后,您将其设置为true。

在您的孩子组件中您可以创建一个输入,例如:

@Input() wasGetXExecuted: boolean;

然后,在儿童组件中您可以实现ngonchanges生命周期挂钩,例如:

ngOnChanges(changes: SimpleChanges) {
    if (changes['wasGetXExecuted'] != null) {
        if (changes['wasGetXExecuted'].previousValue === false &&
            changes['wasGetXExecuted'].currentValue === true) {
            this.getY();
        }
    }
}

您还需要在应用程序组件模板中绑定输入:

<child-component [wasGetXExecuted]="wasGetXExecuted"></child-component>

最新更新