Angular 在 app.module 和 child.module 中导入 FormsModule [英] Angular importing FormsModule in app.module vs child.module

查看:41
本文介绍了Angular 在 app.module 和 child.module 中导入 FormsModule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Angular with lazy loading modules. Each component has its own module. If I import a module in the root module (app.module) it must work fine. For example, I imported HttpClientModule in app.module and can use it in child components.

但是对于 FormsModule,这行不通.我必须在子模块中导入它,否则会出现以下错误:

But about FormsModule, this doesn't work fine. I must import it in the child module, otherwise, I get the following errors:

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

推荐答案

我找到了答案 这里:

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

这篇关于Angular 在 app.module 和 child.module 中导入 FormsModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆