gpt4 book ai didi

Angular6 等待 app.component 中的 ngOnInit 完成,然后在子触发器中等待 ngOnInit

转载 作者:太空狗 更新时间:2023-10-29 19:31:42 27 4
gpt4 key购买 nike

在我的项目中,我想在 app.component 初始化期间调用一个 api,子组件应该等到 app.comonent 在 ngOnInit() 中完成

在 app.component.ts 中:

ngOnInit() {
// call getx()
}

在 child.component.ts 中:

ngOnInit(){
// call gety()
}

执行顺序:希望 gety() 等到 getx() 完成。

最佳答案

也许你可以尝试使用 Promise,比如:

在 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.component

async ngOnInit(): Promise<bool>{

await super.NgOnInit();
// call gety()
resolve(true); //or anything you want ot return maybe from gety()
});
}

希望它能帮助你..也许你也可以用其他方式存档它(比如从你的app.component.ts做一个发射器并在你的子组件中听它......或其他)..希望它能帮助你! !!

关于Angular6 等待 app.component 中的 ngOnInit 完成,然后在子触发器中等待 ngOnInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com