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

前端 未结 6 1030
面向向阳花
面向向阳花 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 04:01

    imports are used to import supporting modules like FormsModule, RouterModule, CommonModule, or any other custom-made feature module.

    declarations are used to declare components, directives, pipes that belong to the current module. Everyone inside declarations knows each other. For example, if we have a component, say UsernameComponent, which displays a list of the usernames and we also have a pipe, say toupperPipe, which transforms a string to an uppercase letter string. Now If we want to show usernames in uppercase letters in our UsernameComponent then we can use the toupperPipe which we had created before but the question is how UsernameComponent knows that the toupperPipe exists and how it can access and use that. Here come the declarations, we can declare UsernameComponent and toupperPipe.

    Providers are used for injecting the services required by components, directives, pipes in the module.

提交回复
热议问题