当我试图运行我的Range2 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">...我出什么问题了吗?
更新:完整代码
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-->发布于 2016-09-05 17:36:46
我在桑基特的回答和评论的帮助下修复了它。
在错误消息中,您不知道也不清楚的是:我将PlannerComponent作为@NgModule.declaration导入到应用程序模块(= RootModule)中。
通过将PlannerModule导入为@NgModule.imports修复了错误。
在此之前:
@NgModule({
declarations: [
AppComponent,
PlannerComponent,
ProfilAreaComponent,
HeaderAreaComponent,
NavbarAreaComponent,
GraphAreaComponent,
EreignisbarAreaComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(routeConfig),
PlannerModule
],
bootstrap: [AppComponent]
})
export class AppModule {之后:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(routeConfig),
PlannerModule
],
bootstrap: [AppComponent]
})
export class AppModule {
}(谢谢你的帮助:)
发布于 2016-10-26 05:02:16
当我将模块A导入模块B,然后尝试使用模块B中的模块A组件时,我收到了此错误。
解决方案是在exports数组中声明该组件。
@NgModule({
declarations: [
MyComponent
],
exports: [
MyComponent
]
})
export class ModuleA {}@NgModule({
imports: [
ModuleA
]
})
export class ModuleB {}发布于 2017-07-04 09:52:27
如果您使用了自动生成的组件定义,您可能会发现选择器的名称中有“app-”。显然,在声明主应用程序组件的子组件时,这是一个新的约定。检查您的选择器是如何在您的组件中定义的,如果您使用了“new”-“component”在角IDE中创建它。所以不要把
<header-area></header-area>你可能需要
<app-header-area></app-header-area>https://stackoverflow.com/questions/39333739
复制相似问题