How to inherit a module from another module in Angular2?

柔情痞子 提交于 2019-12-05 00:41:45
Paul Samsotha

It directly needs access to its own WidgetsModule just like it need direct access to it's own FormsModule (one way or another). One way to clean it up though is to export all the modules that are used in multiple places, from a SharedModule. Then you can just import the SharedModule everywhere

@NgModule({
  exports: [
    FormsModule,
    CommonModule,
    WidgetsModule
  ]
})
class SharedModule {}

You don't need to imports any of them into the SharedModule unless say you are declaring a component in the SharedModule that uses any of those other modules.

Then in all your other modules, just imports the SharedModule. This ends up cleaning it up a lot, and you only need to maintain one shared module.

See Also:

Modules do not inherit access to the components, directives or pipes that are declared in other modules. What AppModule imports is irrelevant to ContactModule and vice versa. Before ContactComponent can bind with [(ngModel)], its ContactModule must import FormsModule.

source: ngmodule

Libu Mathew

I think it's actually not need because WidgetsModule is same as FormsModule (@angular/forms). WidgetsModule is user-defined module and FormsModule provided by angular. That's the only difference. Both of this module are used for doing some functionality.

So just think about how we can declare FormsModule once and use it in all other module, if you understood that then the problem is solved.

Just vist the following link One single NgModule versus multiples ones with Angular 2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!