简体   繁体   English

在每个模块上导入 NgModule 和 FormModule?

[英]NgModule and FormModule importing on every module?

So I'm using RC5, and when embracing things seems to go pretty decently for now I ran into an issue which I'm not sure if I'm doing something wrong or is working as intended;所以我正在使用 RC5,当拥抱事物现在似乎相当不错时,我遇到了一个问题,我不确定我是做错了什么还是按预期工作;

Before it was possible to make form directives etc. globally available.在可以使表单指令等全局可用之前。 Nowadays we started using NgModule.现在我们开始使用 NgModule。 It seems though that I can't use FormsModule and ReactiveFormsModule globally, either in AppModule nor in a SharedModule.似乎我不能在 AppModule 或 SharedModule 中全局使用 FormsModule 和 ReactiveFormsModule。 I need to import it in every single module I create.我需要在我创建的每个模块中导入它。

Is this as intended?这是预期的吗?

According to this NgModule doc it is as intended,根据这个 NgModule 文档,它符合预期,

  • so if you are using multiple NgModule heirarchy in your app, you will need to create a SharedModule and import/export all the global Modules/Components/Directives/Pipes in it.因此,如果您在应用程序中使用多个 NgModule 层次结构,则需要创建一个SharedModule导入/导出其中的所有全局Modules/Components/Directives/Pipes

  • then import this module in every Module that you want to share it with.然后在您想要与之共享的每个模块中导入此模块。


You can read the docs for more clarification.您可以阅读文档以获得更多说明。

You should add FormsModule and ReactiveFormsModule to your SharedModule exports, then you need to import your SharedModule into your other modules.您应该将FormsModuleReactiveFormsModule添加到您的SharedModule导出中,然后您需要将您的SharedModule导入到您的其他模块中。 That way, you will be able to use desired directives globally.这样,您将能够全局使用所需的指令。

I'm using FormsModule and ReactiveFormsModule globally, in my app.module.ts like this:我使用FormsModule和ReactiveFormsModule全球范围内,在我的app.module.ts这样的:

 imports: [ // module dependencies
         BrowserModule, routing,
         FormsModule, ReactiveFormsModule, HttpModule,...
     ]

You can export the class with the directives which you need, an example of this is:您可以使用所需的指令导出类,例如:

  1. Create a file ngforms.module.ts (can be on the root app)创建一个文件ngforms.module.ts (可以在根应用程序上)
  2. Write this:写这个:
 import { NgModule } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; @NgModule({ imports:[ FormsModule, ReactiveFormsModule, ], exports:[ FormsModule, ReactiveFormsModule, ] }) export class NgFormModule {}
  1. In every module or submodule for your app you have to import and call it在您的应用程序的每个模块或子模块中,您必须导入并调用它
import { NgFormModule } from './ngform.module'; imports: [ //Angular material BrowserAnimationsModule, //Angular (forms) NgFormModule ],

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM