尝试运行我的Angular 2 RC6应用程序时,在浏览器控制台中出现以下错误:
> Error: Template parse errors: 'header-area' is not a known element:
> 1. If 'header-area' is an Angular component, then verify that it is part of this module.
> 2. If 'header-area' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component
> to suppress this message.("
<div class="page-container">
[ERROR->]<header-area></header-area>
<div class="container-fluid">
> "): PlannerComponent@1:2
我不明白为什么找不到该组件 . 我的PlannerModule看起来像这样:
@NgModule({
declarations: [
PlannerComponent,
HeaderAreaComponent,
NavbarAreaComponent,
EreignisbarAreaComponent,
GraphAreaComponent,
nvD3
],
imports: [
RouterModule,
CommonModule,
ModalModule
],
bootstrap: [PlannerComponent],
})
export class PlannerModule {}
据我所知,ng2中的模块概念,模块的各个部分在'声明'中声明 . 为了完整性,这里是PlannerComponent:
@Component({
selector: 'planner',
providers: [CalculationService],
templateUrl: './planner.component.html',
styleUrls: ['./planner.component.styl']
})
export default class PlannerComponent {
}
和HeaderAreaComponent:
@Component({
selector: 'header-area',
templateUrl: './header-area.component.html',
styleUrls: ['./header-area.component.styl']
})
export default class HeaderAreaComponent {
}
<header-area>
-Tag位于planner.component.html中:
<div class="page-container">
<header-area></header-area>
<div class="container-fluid">
<div class="row">...
我弄错了吗?
Update :完整的代码
planner.module.ts:
import HeaderAreaComponent from '../header-area/header-area.component';
import NavbarAreaComponent from '../navbar-area/navbar-area.component';
import GraphAreaComponent from '../graph-area/graph-area.component';
import EreignisbarAreaComponent from '../ereignisbar-area/ereignisbar-area.component';
import PlannerComponent from './planner.component';
import {NgModule} from '@angular/core';
import {nvD3} from 'ng2-nvd3';
import {RouterModule} from '@angular/router';
import {CommonModule} from '@angular/common';
import {ModalModule} from 'ng2-bootstrap/ng2-bootstrap';
@NgModule({
declarations: [
PlannerComponent,
HeaderAreaComponent,
NavbarAreaComponent,
EreignisbarAreaComponent,
GraphAreaComponent,
nvD3
],
imports: [
RouterModule,
CommonModule,
ModalModule
],
bootstrap: [PlannerComponent],
})
export class PlannerModule {
// TODO: get rid of the "unused class" warning
}
planner.component.ts
import {Component} from '@angular/core';
import CalculationService from '../_shared/services/calculation.service/calculation.service';
import HeaderAreaComponent from '../header-area/header-area.component';
@Component({
selector: 'planner',
providers: [CalculationService],
templateUrl: './planner.component.html',
styleUrls: ['./planner.component.styl']
})
export default class PlannerComponent {
}
planner.component.html
<div class="page-container">
<header-area></header-area>
<div class="container-fluid">
<div class="row">
<div class="col-xs-2 col-sm-1 sidebar">
<navbar-area></navbar-area>
</div>
<div class="col-xs-10 col-sm-11">
<graph-area></graph-area>
</div>
</div><!--/.row-->
<div class="row">
<div class="col-xs-10 col-sm-11 offset-sm-1">
<ereignisbar-area></ereignisbar-area>
</div>
</div><!--/.row-->
</div><!--/.container-->
</div><!--/.page-container-->
14 回答
具有相同错误消息的另一个可能原因是标记名称和选择器名称不匹配 . 对于这种情况:
<header-area></header-area>
标记名称必须与组件声明中的'header-area'
完全匹配:我将模块A导入模块B时遇到此错误,然后尝试使用模块B中模块A的组件 .
这是在我的
exports
数组中声明该组件的问题 .我在Sanket's answer和评论的帮助下修复了它 .
您在错误消息中无法知道并且不明显的是:我在我的App Module(= RootModule)中将PlannerComponent导入为@ NgModule.declaration .
通过将PlannerModule导入为@NgModule.imports来修复错误 .
之前:
后:
谢谢你的帮助 :)
如果您已经使用了Webclipse自动生成的组件定义,您可能会发现选择器名称前面有“app-” . 显然,这是一个新的约定,用于声明主应用程序组件的子组件 . 如果您使用'new' - 'component'在Angular IDE中创建选择器,请检查您的选择器在组件中的定义方式 . 所以不要放
你可能需要
在 planner 组件中,您必须缺少import HeaderAreaComponent ,如下所示 -
另外,请确保 - All the components and pipes must be declared via an NgModule .
看看这是否有帮助 .
我有角度RC.6的相同问题由于某种原因,它不允许使用指令作为组件装饰器将组件传递给其他组件到父组件
但是如果你通过app模块导入子组件并将其添加到声明数组中,那么错误就会消失 . 没有太多解释为什么这是角度rc.6的问题
当我遇到这个问题时,那是因为我在装饰器中使用了'templateUrl'而不是'template',因为我使用webpack并且需要在其中使用require . 请小心装饰器名称,在我的情况下,我使用片段生成样板代码,装饰器创建为:
但对于webpack,装饰器应该只是'模板' NOT 'templateUrl',如下所示:
改变这一点为我解决了问题 .
想更多地了解这两种方法吗?阅读this medium post about template vs templateUrl
我使用角度5为
<flash-messages></flash-messages>
获取相同的问题 .您只需要在 app.module.ts 文件中添加以下行
NB: 我正在使用这个消息flash-messages
当我有一个文件名和类导出不匹配时,我收到此错误:
filename:list.component.ts
class exported:ListStudentsComponent
从ListStudentsComponent更改为 ListComponent 修复了我的问题 .
一个新手错误在我的情况下生成相同的错误消息 .
index.html中没有
app-root
标记OnInit
在我的类上自动实现,同时在角度CLI上使用ng new ...
关键短语生成新组件 . 因此,在删除实现并删除生成的空方法后,问题得以解决 .对我来说 templateUrl 的路径不正确
我在用
它应该是
愚蠢的错误,但在开始时发生 . 希望能帮助陷入困境的人 .
线程的后期答案,但我更多的人可以使用这个信息在另一个角度解释 .
在Ionic中,自定义角度组件在名为
ComponentsModule
的单独模块下组织 . 当使用ionic generate component
生成第一个组件时,离子与组件一起生成ComponentsModule
. 任何后续组件都会添加到同一模块中,这是正确的 .这是一个样本
ComponentsModule
要在应用中使用
ComponentsModule
,与任何其他角度模块一样,ComponentsModules
需要导入到AppModule
. 离子生成组件(v 4.12)不添加此步骤,因此必须手动添加 .AppModule的摘录:
好的,让我详细介绍代码,如何使用其他模块的组件 .
例如,我有M2模块,M2模块有comp23组件和comp2组件,现在我想在app.module中使用comp23和comp2,这里是如何:
这是app.module.ts,请参阅我的评论,
这是m2模块:
我在代码中的推荐说明了你需要做什么 .
现在在app.component.html中,您可以使用
按照角度doc sample import modul