下面是我的文件夾結構。錯誤:'app-header'不是已知元素:Angular 2
app
- common
- header
header.component.css
header.component.html
header.component.ts
- footer
footer.component.css
footer.component.html
footer.component.ts
- index
index.component.css
index.component.ts
index.component.html
index.module.ts
- lessons
lesson1(another folder)
lesson2(folder)
lesson.module.ts
app.component.css
app.component.ts
app.component.html
app.module.ts
我已導入頭&頁腳部件,index.module,lesson.module在app.module和index.component.html,lesson1.component.html,lesson2使用
<app-header></app-header>
<app-navbar></app-navbar>
。 component.html。
但我得到'應用程序頭'不是已知的元素錯誤。有人可以幫我解決這個錯誤。
它工作得很好,如果我直接在index.module.ts包括頁眉和頁腳部件
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import * as $ from 'jquery';
import { AppComponent } from './app.component';
import { HeaderComponent } from './common/header/header.component';
import { FooterComponent } from './common/footer/footer.component';
import { IndexModule } from './index/index.module';
import { LessonModule } from './index/lesson.module';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule,
IndexModule,
],
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
index.component.html
<app-header></app-header> <app-navbar></app-navbar>
index.module.ts
個import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { IndexComponent } from './index.component';
import { IndexRoutingModule } from './index-routing.module';
@NgModule({
imports: [
CommonModule,
FormsModule,
HttpModule,
IndexRoutingModule
],
declarations: [
IndexComponent
]
})
export class IndexModule { }
lesson.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { Lesson1Component } from './lesson1.component';
import { Lesson2Component } from './lesson2.component';
@NgModule({
imports: [
CommonModule,
FormsModule,
HttpModule,
IndexRoutingModule
],
declarations: [
IndexComponent
]
})
export class IndexModule { }
這將工作。但是頁眉和頁腳組件在所有模塊中都很常見。如果我將來添加課程模塊,那麼它將通過錯誤'header.component'導入索引和課程模塊。請將其移至已導入索引和課程模塊的較高模塊。 –
創建一個新的共享模塊,其中包含您將導入並在其中聲明的所有共享組件。然後在索引模塊,應用程序模塊等中導入該模塊。 – Vega
我是否需要在'common'文件夾下創建該公共模塊,並將其導入'app.module'中? –