我想創建一個簡單的組件並將其包含在頁面中。我使用ionic g component my-header
(ionic-cli v3 beta)創建了它,修復了IonicPageModule錯誤,然後添加到另一個頁面。然後我得到這個錯誤:離子v3中的自定義組件
Error: Uncaught (in promise): Error: Template parse errors:
'my-header' is not a known element:
1. If 'my-header' is an Angular component, then verify that it is part of this module.
2. If 'my-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
的「MyHeaderComponent」被自動添加到@NgModule聲明。
感謝您的幫助。
編輯:
該組件位於我的components
文件夾內:
組件/我的報頭/我的-了header.html
<div>
{{text}}
</div>
組件/我的報頭/我的報頭.module.ts
import { NgModule } from '@angular/core';
import { IonicModule } from 'ionic-angular';
import { MyHeaderComponent } from './my-header';
@NgModule({
declarations: [
MyHeaderComponent,
],
imports: [
IonicModule,
],
exports: [
MyHeaderComponent
],
entryComponents:[
MyHeaderComponent
]
})
export class MyHeaderComponentModule {}
components/my-header/my-header.scss
my-header {}
組件/我的頭/我-header.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-header',
templateUrl: 'my-header.html'
})
export class MyHeaderComponent {
text: string;
constructor() {
console.log('Hello MyHeaderComponent Component');
this.text = 'Hello World';
}
}
應用程序/ app.module.ts
/* imports */
import { MyHeaderComponent } from '../components/my-header/my-header';
@NgModule({
declarations: [
MyApp,
MyHeaderComponent
],
...
頁/家庭/ home.html的
你的組件有單獨的module.ts文件嗎? –
是的,我添加了與組件相關的整個代碼 –