Angular 2找不到在我的功能模块中声明的组件 [英] Angular 2 can't find a component declared in my feature module

查看:62
本文介绍了Angular 2找不到在我的功能模块中声明的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让模块在Angular 2中工作.我创建了一个小问题来演示此问题.在小插曲中,您会看到我有app.module.此模块导入app-common.module,其中包含一个用于显示页面标题的组件.顶级组件的模板app.component包含此组件的选择器.这是app.common.module:

I'm having a tough time getting modules to work in Angular 2. I have created a plunk that demonstrates the problem. In the plunk, you'll see I have app.module. This module imports app-common.module, which contains a component to display page headers. The template for the top level component, app.component contains the selector for this component. Here's app.common.module:

@NgModule({
imports:[CommonModule, FormsModule],
declarations: [PageHeaderComponent]
})
export class AppCommonModule { }

这是app.module:

Here's app.module:

@NgModule({
imports:      [AppCommonModule, BrowserModule],
declarations: [AppComponent ],
bootstrap:    [ AppComponent ]
})
export class AppModule { }

运行该应用程序时,它将引发错误,即"ref-pageheader"不是已知元素.如果我在app.module中声明该组件,则可以正常工作.那么,为什么不能在导入到主app.module的模块中声明此组件?看来完成后Angular无法找到它.我究竟做错了什么?我想念什么吗?

When the application is run, it throws an error that "ref-pageheader" is not a known element. If I declare the component in app.module, it works fine. So, why can't I declare this component in a module that gets imported into the main app.module? It seems Angular can't find it when this is done. What am I doing wrong? Am I missing something?

推荐答案

我想您应该将其导出为:

I guess you should export it like:

@NgModule({
    imports:[CommonModule, FormsModule],
    declarations: [PageHeaderComponent],
    exports: [PageHeaderComponent]
})
export class AppCommonModule { } 

这样其他组件可以使用该组件.

This way other components could use the component.

否则, PageHeaderComponent 仅在 AppCommonModule

另请参见

我们导出ContactComponent,以便其他模块导入ContactModule可以将其包含在其组件模板中.

We export the ContactComponent so other modules that import the ContactModule can include it in their component templates.

所有其他声明的联系人类别默认为私有

All other declared contact classes are private by default

  • https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-要出口什么
  • 导出应声明的类,其他模块中的组件应为能够在其模板中引用.这些是您的公开课.如果您不导出课程,则该课程将保持私有状态,仅对其他人可见该模块中声明的组件.

    Export declarable classes that components in other modules should be able to reference in their templates. These are your public classes. If you don't export a class, it stays private, visible only to other component declared in this module.

    这篇关于Angular 2找不到在我的功能模块中声明的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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