【问题标题】:Angular2 app.moduleAngular2 app.module
【发布时间】:2018-01-29 05:34:05
【问题描述】:

如果我们在 app.module 中导入 FormsModule 那么我们还需要在其他子模块中导入它才能在子模块中使用它吗?

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    SignupComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpModule,
    FormsModule,
  ],
  providers: [RestaurantsService, LoginAuthService],
  bootstrap: [AppComponent]
})
export class AppModule { }

【问题讨论】:

    标签: angular


    【解决方案1】:

    是的,你必须这样做。

    但是,如果您发现自己在不同的子模块中重新导入相同的模块,您可以将常用模块导入到 CommonModule 中,该模块将导出这些模块

    @NgModule({
      imports: [FormsModule],
      exports: [FormsModule]
    })
    export class SharedModule {}  
    

    然后,在您的其他子模块中,只需导入您的 CommonModule,您将不再需要导入 FormsModule。

    https://angular.io/guide/ngmodule-faq#what-should-i-export

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 2020-07-11
      • 2017-02-02
      • 1970-01-01
      • 2018-03-11
      • 2022-08-17
      • 2018-10-20
      • 2017-04-05
      • 1970-01-01
      相关资源
      最近更新 更多