【问题标题】:Can't bind to 'ngForOf' since it isn't a known property of 'ion-item'无法绑定到“ngForOf”,因为它不是“ion-item”的已知属性
【发布时间】:2021-01-06 13:46:49
【问题描述】:

我正在尝试使用 ngFor 来遍历列表中的项目。但是,我在浏览器上收到此警告,并且列表未显示。我该如何解决?我已经在app.module.ts 中的@NgModule 导入中添加了BrowserModuleCommonModule。这并没有解决问题。

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

import { CommonModule } from "@angular/common"; //if we want to use ngFor and other types //then we need to include common module to be able to use it


@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    CommonModule,
    IonicModule.forRoot(),
    AppRoutingModule 
  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

锻炼计划.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>workoutplan !!</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-list> 
    <ion-item  *ngFor ="let workoutplan of workoutPlans">
      {{workoutplan.title}}
    </ion-item>
  </ion-list>
</ion-content>

【问题讨论】:

    标签: angular ionic-framework ionic4 ngfor


    【解决方案1】:

    此错误的唯一原因可能是在不属于AppModule 的组件中使用*ngFor。我假设您在使用某些不同模块(不是 AppModule 或根模块)注册的组件中使用 *ngFor

    只需在该特定模块中导入 CommonModule 并将其添加到 imports 数组中。

    import { CommonModule } from '@angular/common';
    
    imports: [CommonModule]
    

    【讨论】:

    • 这是正确的。导入需要在声明WorkoutPage的模块中
    • 我之前也试过,没有解决问题。
    • 能否请您在注册锻炼计划组件的地方添加模块代码。
    • 理解第一段让我朝着正确的方向前进。
    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 2021-03-20
    • 2021-10-24
    • 2019-10-09
    • 2020-06-17
    • 2016-06-02
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多