简体   繁体   中英

Why dont we add 'ngModule' in decorator array of app.module.ts

I am making an angular project and have made required components and imported modules too. The main issue I see here ... is that we make 2 steps:

  1. Add component, services, pipes, modules to the top adding respective files or folder locations
  2. Add them in decorator (components, pipes, models go to declarations), (Modules to imports) and (services to providers).

There is an exception. Now, I expected that just like ' browser-module ' (pre-fetched) or 'forms-module' (added manually) -- in the step-2 (ie, decorator array) there should be an injection or name-addition for ' ng-Module '. Why is it not there? Is this only exception or there is some other too? Shouldn't 'ngModule' be added in decorator would have been a consistent way?

在此处输入图片说明

Note: Code is not required as app it is working fine. The question is simple that if we are using a particular rule for all -- Components, Services, Modules, Pipes, Models and Interface -- why not for 'ngModule'?

NgModule is a class marked by the @NgModule decorator. It is not a separate Module that is configured in your application.

It is required because you use @NgModule decorator at definition of your module. It's just a decorator and not a module. You need to import its script because you simply use it. You can think of it as @Input or @Output decorators. You import them within your components just to use it within component.

You can look at the code of NgModule

It is an interface

export interface NgModule { ...

However, BrowserModule is a module that decorates itself with @NgModule

Check the code

@NgModule({providers: BROWSER_MODULE_PROVIDERS, exports: [CommonModule, ApplicationModule]})
export class BrowserModule { ...

AOT Complier contains 2 phases.

  1. Code Analysis Phase 2) Code Generation Phase

During Code Analysis Phase, with the help @ngModule class decorator AOT Compiler will get the metadata about the module. Using these details, it will create metadata.json for Compilation.

During Code Generation Phase, it will create module factories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM