Angular 错误 NG0303 在 Angular 项目的 app.component.html 中使用 NGFor

问题描述 投票:0回答:1

我想在 Angular 中使用 for 循环,这是我在

app.component.ts 

中的代码

export class AppComponent {
  title = 'CatsBookProject';


    postText =[
      'Hello , i am here to meet new friends and make a friends relationship',
      'Hello everyone, Iam glad you are here',
      'Hey, my name is Susanne. I amm 2 years old',
         'I like eating cookies.'
     ]
    postImgs = [
      'assets/Imge/img1.png',
      'assets/Imge/im21.png',
      'assets/Imge/img3.jpg',
      'assets/Imge/img4.jpg',
     ]


   buttonClicked(){
    alert("What's up");
  }
}

还有这个在

app.componwnt.html

 <app-stroy *ngFor='let i of [0, 1, 2, 3]' [img]='postImgs[0]' [text]='postText[0]'></app-stroy> 

我有这个错误

NG0303: Can't bind to 'ngForOf' since it isn't a known property of 'app-stroy' (used in the '_AppComponent' component template).
1. If 'app-stroy' is an Angular component and it has the 'ngForOf' input, then verify that it is included in the '@Component.imports' of this component.
2. If 'app-stroy' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@Component.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@Component.schemas' of this component.

也许有人知道为什么? 我需要一些帮助。

angular typescript ngfor
1个回答
1
投票

组件导入了吗?取决于您的 Angular 版本,如果您使用非独立组件,则需要将该组件添加到根模块的声明数组中。如果您使用独立组件,则需要将其添加到 AppComponent 的导入数组中。另外,请分享一下你的AppStroyComponent,是否确认你真的使用的是selector: app-stroy?

© www.soinside.com 2019 - 2024. All rights reserved.