What is the difference between declarations, providers, and import in NgModule?

前端 未结 6 1045
面向向阳花
面向向阳花 2020-12-02 03:39

I am trying to understand Angular (sometimes called Angular2+), then I came across @Module:

  1. Imports

  2. Declarations

  3. <
6条回答
  •  盖世英雄少女心
    2020-12-02 03:56

    Adding a quick cheat sheet that may help after the long break with Angular:


    DECLARATIONS

    Example:

    declarations: [AppComponent]

    What can we inject here? Components, pipes, directives


    IMPORTS

    Example:

    imports: [BrowserModule, AppRoutingModule]

    What can we inject here? other modules


    PROVIDERS

    Example:

    providers: [UserService]

    What can we inject here? services


    BOOTSTRAP

    Example:

    bootstrap: [AppComponent]

    What can we inject here? the main component that will be generated by this module (top parent node for a component tree)


    ENTRY COMPONENTS

    Example:

    entryComponents: [PopupComponent]

    What can we inject here? dynamically generated components (for instance by using ViewContainerRef.createComponent())


    EXPORT

    Example:

    export: [TextDirective, PopupComponent, BrowserModule]

    What can we inject here? components, directives, modules or pipes that we would like to have access to them in another module (after importing this module)

提交回复
热议问题