Angular 获取组件是未知元素错误,尽管我已导入该组件
我试图学习Angular(通过使用Ionic创建一个应用程序),我想添加一个我将创建的自定义组件。我使用了Angular 获取组件是未知元素错误,尽管我已导入该组件,angular,ionic-framework,Angular,Ionic Framework,我试图学习Angular(通过使用Ionic创建一个应用程序),我想添加一个我将创建的自定义组件。我使用了ng generate来生成一个新组件,但是当我尝试使用它时,在另一个组件中,我得到一个错误,上面写着: `'GoalCardComponent' is not a known element: 1. If 'GoalCardComponent' is an Angular component, then verify that it is part of this module. 2. T
ng generate
来生成一个新组件,但是当我尝试使用它时,在另一个组件中,我得到一个错误,上面写着:
`'GoalCardComponent' is not a known element:
1. If 'GoalCardComponent' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`
我试着搜索,看到了一些关于这方面的线索,但没有任何效果(我想可能框架中的情况发生了变化?)
这是GoalCardComponent:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-goal-card',
templateUrl: './goal-card.component.html',
styleUrls: ['./goal-card.component.scss'],
})
export class GoalCardComponent implements OnInit {
constructor() { }
ngOnInit() {}
}
这是主组件:
import { Component, NgModule } from '@angular/core';
import {GoalCardComponent} from '../goal-card/goal-card.component';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
@NgModule({
entryComponents: [
GoalCardComponent
],
})
export class HomePage {
greetingTitle = "Good morning"
motivationQuote = "A journey of a thousand miles begins with a single step"
constructor() {}
createGoal() {
console.log("Clicked create goal")
}
}
这是主组件html:
<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-label id="greetingTitle">
{{greetingTitle}}
</ion-label>
<br/>
<ion-label id="motivationQuote">
{{motivationQuote}}
</ion-label>
</ion-header>
<app-goal-card>
</app-goal-card>
<ion-fab vertical="bottom" horizontal="end" slot="fixed" style="padding: 8pt;">
<ion-fab-button (click)="createGoal()">
<ion-icon name="add"></ion-icon>
</ion-fab-button>
</ion-fab>
</ion-content>
{{greetingTitle}}
{{motivationQuote}
您需要将自定义组件添加到appModule
的entryComponents
和声明中:
import { GoalCardComponent } from '../goal-card/goal-card.component';
@NgModule({
declarations:[
GoalCardComponent
],
entryComponents:[
GoalCardComponent
],
})
谢谢,我试过了,但还是出现了同样的错误。你能在stackblitz上提供你的代码吗?谢谢,我在这里添加了代码(这仅仅是一个开始,我几乎没有做任何更改)。希望这足够好。是否可以将整个应用程序导入stackblitz?因为我找不到方法将我的文件复制到那里。下面是一个带有GoalCardComponent的stackblitz:。创建组件后,您需要将其添加到声明中的app.module.ts
和entryComponents
数组中,我尝试了这个,但遇到了相同的问题。但是,将导入添加到home.module.ts是有效的(而在更改之前它不起作用),所以我想现在可以了。谢谢