What is entryComponents in angular ngModule?

前端 未结 6 1781
[愿得一人]
[愿得一人] 2020-12-04 06:31

I am working on an Ionic app ( 2.0.0-rc0 ) which depends on angular 2 . So the new introduction of ngModules is included.

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 06:52

    You won't get explanation better than Angular docs: entry-components and ngmodule-faq.

    And below is the explanation from the angular docs.

    An entry component is any component that Angular loads imperatively by type.

    A component loaded declaratively via its selector is not an entry component.

    Most application components are loaded declaratively. Angular uses the component's selector to locate the element in the template. It then creates the HTML representation of the component and inserts it into the DOM at the selected element. These aren't entry components.

    A few components are only loaded dynamically and are never referenced in a component template.

    The bootstrapped root AppComponent is an entry component. True, its selector matches an element tag in index.html. But index.html isn't a component template and the AppComponent selector doesn't match an element in any component template.

    Angular loads AppComponent dynamically because it's either listed by type in @NgModule.bootstrap or boostrapped imperatively with the module's ngDoBootstrap method.

    Components in route definitions are also entry components. A route definition refers to a component by its type. The router ignores a routed component's selector (if it even has one) and loads the component dynamically into a RouterOutlet.

    The compiler can't discover these entry components by looking for them in other component templates. You must tell it about them by adding them to the entryComponents list.

    Angular automatically adds the following types of components to the module's entryComponents:

    • The component in the @NgModule.bootstrap list.
    • Components referenced in router configuration.

    You don't have to mention these components explicitly, although doing so is harmless.

提交回复
热议问题