如何推迟主组件对依赖组件的初始化,以便解决某些承诺 [英] How to defer Main component's initialization of dependent components so that some promises may get resolved
问题描述
我正在申请angular2应用程序.我最大的问题是无法将主要组件的依赖子组件的加载推迟到解决某些诺言之前.
I am making an angular2 application. My biggest problem is the inability to defer loading of main component's dependent child component till some promise gets resolved.
我有一个名为AppComponent的app.component.ts,我在boot.ts中这样引导:
I have app.component.ts, with class named AppComponent, which I bootstrap in boot.ts like this:
bootstrap(AppComponent, [ROUTER_PROVIDERS, HTTP_PROVIDERS]);
现在,在导航到我的默认路由(即-'/')之前,我想调用一些非常重要的http服务.一项http服务会将数据返回给我,这些数据将用于执行SEO并在各种路由上设置元数据.
Now I want to call few very vital http services before navigating to my default route, i.e-'/'. One http service returns data to me that will be used for doing SEO and setting meta data on various routes.
我知道首先会调用任何类的构造函数,但是我不知道构造函数是否等待promise解析.
I know that the constructor of any class is called at first, but I don't know if the constructor waits for the promises to get resolved.
推荐答案
另一个解决方案是在构造函数中使用计时器:
another solution is using a timer in the constructor:
setTimeout(() => {
call your function here
}, 500);
这篇关于如何推迟主组件对依赖组件的初始化,以便解决某些承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!