【发布时间】:2017-09-16 09:29:48
【问题描述】:
我想创建一个简单的组件并将其包含在页面中。我使用ionic g component my-header(ionic-cli v3 beta)创建了它,修复了 IonicPageModule 错误,然后添加到另一个页面。然后我得到这个错误:
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 文件夹中:
components/my-header/my-header.html
<div>
{{text}}
</div>
components/my-header/my-header.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 {}
components/my-header/my-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/app.module.ts
/* imports */
import { MyHeaderComponent } from '../components/my-header/my-header';
@NgModule({
declarations: [
MyApp,
MyHeaderComponent
],
...
pages/home/home.html
【问题讨论】:
-
您的组件有单独的 module.ts 文件吗?
-
是的,我添加了与组件相关的整个代码
-
不要认为这是一个答案或者它值得一个新问题,但我来到这里是因为我的延迟加载页面出现了这个错误。除了将
ComponentsModule导入页面模块的公认答案外,我还必须将组件添加到组件模块的entryComponents中(如问题所示)。