为什么延迟加载的模块必须导入commonModule?角度2 [英] why lazy loaded module has to import commonModule? Angular 2

查看:179
本文介绍了为什么延迟加载的模块必须导入commonModule?角度2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在应用程序的根模块中导入 BrowserModule 时,可以在热切加载的组件中使用NgIf和NgFor().但是对于延迟加载的模块,我必须导入CommonModule,该模块已由root的BrowserModule导出.那么为什么我们必须再次将其导入到延迟加载的模块中?

When we import BrowserModule in root module of application ,we can use NgIf and NgFor( in eagerly loaded component). But for lazy loaded modules I have to import CommonModule which has been exported by BrowserModule of root. SO why do we have to import it again in lazy loaded module?

推荐答案

正如沃德·贝尔(Ward Bell)所说(

As Ward Bell said(https://devchat.tv/adv-in-angular/119-aia-avoiding-common-pitfalls-in-angular2):

只要您的应用程序中只有一个模块并且您扔了 那里的所有东西,您都从通用模块隐藏中受益 在浏览器模块中. 创建新模块的那一刻,无论是懒惰还是 不,任何新模块,而您在其中声明任何内容,则该新模块 处于干净状态.它不了解任何内容,无论是Angular还是 任何事物.它是任何模块,而不是您在其中声明的惰性模块 某些东西,您将必须导入您实际上所有的东西 需要您在其中声明的任何组件.这就是为什么你 所需的通用模块

As long as you only have one module in your application and you threw everything in there, you were benefiting from the common module hiding inside the browser module. The minute you create a new module, lazy or not, any new module and you declare anything into it, that new module has a clean state. It has no knowledge of anything, Angular or anything. It’s any module, not a lazy module in which you declare something, you’re going to have to import everything that you actually need for any of the component you declared in it. That’s why you needed common module

模块不会继承对其他模块中声明的组件,指令或管道的访问.(这就是为什么必须导入CommonModule才能访问ngIfngFor等指令的原因.您的模块对其他模块的指令一无所知.它仅从导入的模块查看其declarationsexports

That's why you have to import CommonModule to have access to ngIf, ngFor and so on directives. Your module doesn't know anything about directives from other modules. It only looks at its declarations and exports from imported modules

另请参阅:

这篇关于为什么延迟加载的模块必须导入commonModule?角度2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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