Angular2 providers in bootstrap vs @component

删除回忆录丶 提交于 2019-12-05 13:38:11

Angular2 DI always looks upwards for providers of a requested dependency. If a service that is instantiated by bootstrap requires a dependency than it doesn't get one injected that is provided further down the tree.

Providing at bootstrap(...) and @Component(...) of the root component are not equivalent but this distinction is not relevant for everything inside the root component or one of its children or other descendants. bootstrap() is one level higher than the root component.

The Angular2 style-guide also suggests to prefer the root component over bootstrap() because bootstrap should be reserved for system stuff.

Forms and the router need to be instantiated before the first component is created (there was for example a bug in early versions of the V3 router where the root component needed to inject Router or contain a routerLink, otherwise the router wouldn't start up).

So because some things need to be instantiated before the root component gets created the situation comes up where the difference between bootstrap() and root component becomes relevant.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!