Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

angular - How to defer Main component's initialization of dependent components so that some promises may get resolved

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.

I have app.component.ts, with class named AppComponent, which I bootstrap in boot.ts like this:

bootstrap(AppComponent, [ROUTER_PROVIDERS, HTTP_PROVIDERS]);

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could bootstrap asynchronously after your request(s) complete(s). Here is a sample:

var app = platform(BROWSER_PROVIDERS)
   .application([BROWSER_APP_PROVIDERS, appProviders]);

var service = app.injector.get(CompaniesService);

service.executeSomething().flatMap((data) => {
  var companiesProvider = new Provider('data', { useValue: data });
  return app.bootstrap(appComponentType, [ companiesProvider ]);
}).toPromise();

See this question for more details:

and the corresponding plunkr: https://plnkr.co/edit/ooMNzEw2ptWrumwAX5zP?p=preview.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...