首页 > 解决方案 > Ionic 中的 nfFor 命令在组件内不起作用

问题描述

我正在 Ionic / Angular 中创建一个 Ionic 应用程序。我创建了一些页面并且工作正常。但是现在我正在尝试创建一个组件,但我不能使用 *ngFor 命令。

我使用 Ionic cli 创建了组件。这是我的 HTML:

<ion-item button *ngFor="let myItem of myCollection">
    (...)
</ion-item> 

我已经在页面中使用了 ngFor 并且它有效,但在组件内部知道我收到以下错误:

core.js:10140 NG0303: Can't bind to 'ngForOf' since it isn't a known property of 'ion-item'.

我已经搜索过这个问题,并建议将 BrowserModule 放在 app 中,将 CommonModule 放在我的页面模块中。我的应用程序已经有了它,每个页面也都有,但是我的组件没有模块文件,我应该把它放在哪里?或者我该如何解决这个问题?

这些是我正在使用的版本:

Ionic CLI                     : 6.16.1 (C:\Users\User\AppData\Roaming\npm\node_modules\@ionic\cli)
Ionic Framework               : @ionic/angular 5.6.9
@angular-devkit/build-angular : 0.1102.14
@angular-devkit/schematics    : 11.2.14
@angular/cli                  : 11.2.14
@ionic/angular-toolkit        : 3.1.1

标签: angularionic-framework

解决方案


由于您的组件没有模块,因此您需要在要添加组件的页面模块内的声明中添加组件。请参阅下面的示例代码:

@NgModule({
  imports: [
    BrowserModule,
    CommonModule,
    ....
  ],
  declarations: [
    UserComponent // Add the components name to the module declarations
  ]
})

export class SamplePageModule {}

推荐阅读