简体   繁体   English

在Angular中使用NgModule中的类

[英]use of class in NgModule in Angular

What is the use of the class in NgModule in Angular? 在Angular中NgModule中的类有什么用? In @Component, the class can contain variables of the component. 在@Component中,类可以包含组件的变量。 What useful information could the class in NgModule contain? NgModule中的类可以包含哪些有用的信息?

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Your class decorated with @NgModule will be empty, it is a class just to be exported to main.ts (probably), to bootstrap your application. 用@NgModule修饰的类将为空,它是一个类,只是要导出到main.ts(可能),以引导您的应用程序。

And others Modules are exported as classes so they can be imported into your root module. 其他模块作为类导出,因此可以将它们导入到根模块中。

I use module class constructor for some configuration initialization: 我使用模块类构造函数进行一些配置初始化:

@NgModule({...})
export class RecordEditorViewComponentModule {
  constructor(private viewComponent: ViewComponent) {
    viewComponent.registerComponent({

You can also use module methods to register the same module with different set of providers so several instances of a module will be instantiated. 您还可以使用模块方法向不同的提供程序集注册相同的模块,以便实例化模块的多个实例。 For example, the way routing module does that. 例如,路由模块的方式。 Router module provides two distinct methods to register module - forRoot and forChild . 路由器模块提供两种不同的方法来注册模块 - forRootforChild They both register the same module but with different set of providers. 它们都注册相同的模块,但具有不同的提供者集。 See this answer for more details. 有关详细信息,请参阅此答案

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

相关问题 Angular 2 Dart:在Dart中使用NgModule - Angular 2 Dart: use of NgModule in dart 不能将实体类与NgModule Angular 2一起使用 - Cannot use entity classes with NgModule Angular 2 在NgModule绑定angular4上使用get / set - Use get/set on NgModule binding angular4 如何在angular2 NgModule中使用服务 - How to use service in angular2 NgModule Angular 9 或 10 + Angular 材料 10:导入无法解析为 NgModule class - Angular 9 or 10 + Angular Material 10: import could not be resolved to an NgModule class 出现在 AppModule 的 NgModule.imports 中,但无法解析为 NgModule class. in Angular 12.2.9 - Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class. in Angular 12.2.9 Angular 错误NG6002:出现在AppModule的NgModule.imports中,但无法解析为NgModule class - Angular error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class Angular:HttpClientModule 导入错误(无法解析为 NgModule 类) - Angular: HttpClientModule import error (could not be resolved to an NgModule class) 如何使用Angular 2 RC6和NgModule导出抽象类 - How to export abstract class using Angular 2 RC6 and NgModule 构建 Angular 应用程序时,获取“未找到“类{}”错误的“未找到 NgModule 元数据” - Getting `No NgModule metadata found for 'class{}'` error when building angular application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM