Angular2 RC6 - 在我的子模块中导入 BrowserModule [英] Angular2 RC6 - importing BrowserModule in my submodules

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

问题描述

我在我的根 AppModule 中导入了 DashboardModule.在来自 DashboardModule 的组件模板中,我使用了 *ngFor.它在 BrowserModule 中声明(准确地说是在 BrowserModule 导入的 CommonModule 中).

I have DashboardModule imported in my root AppModule. In a component template from DashboardModule I use *ngFor. It is declared in BrowserModule (precisely in CommonModule imported by BrowserModule).

我想在我的应用程序的根模块中声明一次 BrowserModule 的导入.但是有些东西不起作用,我还必须在 DashboardModule 中导入它.当我注释掉导入时,控制台中出现错误 Can't bind to 'ngForOf' 因为它不是 ... 的已知属性.

I'd like to declare importing of BrowserModule once in my application in root module. But something is not working and I must import it also in DashboardModule. When I comment out importing, I have error in console Can't bind to 'ngForOf' since it isn't a known property of ....

这是 plunker,目前正在工作:http://plnkr.co/edit/73SYTnsMJxoVl5OSvWiF?p=preview(由于使用控制台,Chrome 中的最佳视图).

Here is the plunker, currently working: http://plnkr.co/edit/73SYTnsMJxoVl5OSvWiF?p=preview (best view in Chrome because of console usage).

编辑文件 src/dashboard/dashboard.module.ts - 注释第 18 行(导入 BrowserModule)并且错误在控制台中可见.

Edit file src/dashboard/dashboard.module.ts - comment line 18 (importing BrowserModule) and error is visible in console.

推荐答案

你应该不要在子模块中导入BrowserModule.

You should not import BrowserModule in submodule.

在根模块中导入BrowserModule,在子模块中导入CommonModule.

Import BrowserModule in the root module and CommonModule in submodules.

Plunker 示例

另见https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#q-browser-vs-common-module

不要在任何其他模块中导入 BrowserModule.功能模块和延迟加载模块应该导入 CommonModule 代替.他们需要通用指令.他们不需要重新安装应用范围的提供程序.

Do not import BrowserModule in any other module. Feature modules and lazy loaded modules should import CommonModule instead. They need the common directives. They don't need to re-install the app-wide providers.

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

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