在app.module和child.module中角度导入FormsModule

问题描述 投票:0回答:2

我正在使用Angular和延迟加载模块。每个组件都有自己的模块。如果我在根模块(app.module)中导入模块,它必须正常工作。例如,我在app.module中导入了HttpClientModule,可以在子组件中使用它。

但关于FormsModule,这不行。我必须在子模块中导入它,否则,我会收到以下错误:

Can't bind to 'ngModel' since it isn't a known property of 'input'. ("

Do you have any idea why this happens?
angular angular-ngmodel
2个回答
2
投票

我找到了答案here

  • 如果为组件导入模块,则需要在需要它们的每个模块中导入它,
  • 如果为服务导入模块,则只需在第一个应用程序模块中导入一次。

1
投票

创建SharedModule

@NgModule({

 imports: [
    CommonModule,
    FormsModule,
  ],
  declarations: [
  ],
  exports: [
    CommonModule,
    FormsModule,
  ]
})
export class SharedModule {
}

并将其添加到app.module.ts

 imports: [ SharedModule.forRoot(),
// Core Module
CoreModule.forRoot()]
© www.soinside.com 2019 - 2024. All rights reserved.