简体   繁体   English

我们为什么不在app.module.ts的装饰器数组中添加“ ngModule”

[英]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 '. 现在,我希望就像“ 浏览器模块 ”(预提取)或“表单模块”(手动添加)一样,在步骤2(即装饰器数组)中,应该为' 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? 不应该在装饰器中添加“ ngModule”是一致的方式吗?

在此处输入图片说明

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”使用?

NgModule is a class marked by the @NgModule decorator. NgModule是由@NgModule装饰器标记的类。 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. 这是必需的,因为在模块定义时使用@NgModule装饰器。 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. 您可以将其视为@Input@Output装饰器。 You import them within your components just to use it within component. 您将它们导入到组件中只是为了在组件中使用它。

You can look at the code of NgModule 您可以看一下NgModule代码

It is an interface 这是一个接口

export interface NgModule { ...

However, BrowserModule is a module that decorates itself with @NgModule 但是, BrowserModule是使用@NgModule装饰自身的模块

Check the code 检查代码

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

AOT Complier contains 2 phases. AOT编译器包含2个阶段。

  1. Code Analysis Phase 2) Code Generation Phase 代码分析阶段2)代码生成阶段

During Code Analysis Phase, with the help @ngModule class decorator AOT Compiler will get the metadata about the module. 在代码分析阶段,借助@ngModule类装饰器,AOT编译器将获取有关模块的元数据。 Using these details, it will create metadata.json for Compilation. 使用这些详细信息,它将为编译创建metadata.json。

During Code Generation Phase, it will create module factories. 在代码生成阶段,它将创建模块工厂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么要在 app.module.ts 中导入 { HttpClientModule } - Why to import { HttpClientModule } in app.module.ts 有没有办法在App.Module.ts中使用configService? - Is there a way to use configService in App.Module.ts? Angular:app.module.ts 中的奇怪 if 语句行为 - Angular: Weird if statement behaviour in app.module.ts Angular 6如何将配置传递到app.module.ts中的外部库 - Angular 6 how to pass the configuration to the external library in app.module.ts Angular 2如何将配置从app.module.ts传递给提供程序 - Angular 2 how to pass config to provider from app.module.ts 如何将数据从 app.component.ts 传递到 Angular 中的 app.module.ts? - How to pass data from app.component.ts to its app.module.ts in Angular? 如何在 angular 模块 app.module.ts 中加载 highcharts 注释模块? - How to load highcharts annotations module in angular module app.module.ts? mat-button-toggle' 不是已知元素:即使在将导入添加到 app.module.ts 之后 - mat-button-toggle' is not a known element: Even after adding imports to app.module.ts 角度材料中的动画不起作用。 问题不在于 NoopAnimationsModule 或 app.module.ts 中的材质或导出版本 - Animations in angular material not working. Problem isn't NoopAnimationsModule or version of material or exports in app.module.ts 在 Angular 8 /9 中,我可以在 app.module.ts 中确定我的 URL 并在声明模块时使用它吗? - In Angular 8 /9, can I determine my URL inside of app.module.ts and use it when declaring modules?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM