在Angular2中继承从父模块​​到子模块的导入 [英] Inherit imports from parent module to child module in Angular2

查看:120
本文介绍了在Angular2中继承从父模块​​到子模块的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个Angular2应用,如所述这里.它具有两个由全局app.module导入的组件(A,B).我的想法是在app.module中包含共享模块,因此我不需要使用冗余代码来弄乱每个模块.我想用FormsModule来做到这一点.所以在app.module我有

I made an Angular2 app like described here. It has two components (A,B) which are imported by the global app.module. My idea was, to include shared modules in app.module, so I don't need to mess up every module with redundant code. I want to do that for example with the FormsModule. So in the app.module I have

imports: [
    UniversalModule,
    CommonModule,
    FormsModule,
    AModule
    RouterModule.forRoot([])
],

exports: [FormsModule]

但是在A模块中,我得到了异常Can't bind to 'ngModel' since it isn't a known property of 'select'.,这似乎是由于缺少FormsModule引起的.仅当我也使用imports: [FormsModule]在每个子模块中导入FormsModule时,此方法才有效.这正是我要避免的事情.

But in the A module I got the exception Can't bind to 'ngModel' since it isn't a known property of 'select'. which seems caused by the missing FormsModule. It only works, when I import the FormsModulein every child module too using imports: [FormsModule]. That's exactly what I want to avoid.

根据这个问题,我尝试将AppModule导入子模块A.这不起作用,并给我Exception: Call to Node module failed with error: Error: Unexpected value 'undefined' imported by the module 'AModule'

According to this question, I tried to import the AppModule in the child module A. This doesn't work and give me the exception Exception: Call to Node module failed with error: Error: Unexpected value 'undefined' imported by the module 'AModule'

如何继承对子模块的导入?我也需要管道.

How can I inherit the imports to child modules? I need this for pipes, too.

推荐答案

只需创建一个功能模块(或共享模块),即可导出通常一起使用的组件,指令和管道,并将此模块导入到您在其中使用的模块想要使用其中任何一个.

Just create a feature module (or shared module) that exports the components, directives, and pipes, that are usually used together and import this module to modules where you want to use any of these.

无法使组件,指令或管道全局可用.需要将它们添加到使用它们的每个模块的导入中. 您所要做的就是合并模块,并通过仅导入一个或几个模块来使它们可用.

There is no way to make components, directives, or pipes available globally. They need to be added to imports of every module where they are used. All you can do is combine modules and make them available by importing only a single or few modules.

这篇关于在Angular2中继承从父模块​​到子模块的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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