Angular 17 错误:无法绑定到 'ngForOf',因为它不是 'li' 的已知属性

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

这是抛出错误的代码: user-list.component.html:

<ul>
    <li *ngFor="let name of names">Hello {{ name }}</li>
</ul> 

错误:

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

而app.component.ts:

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { HelloWorldComponent } from './hello-world/hello-world/hello-world.component';
import { UserItemComponent } from './user-item/user-item.component';
import { UserListComponent } from './user-list/user-list.component';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet, HelloWorldComponent,UserItemComponent, UserListComponent],
  templateUrl: './app.component.html',
  styleUrl: './app.component.css'
})
export class AppComponent {
  title = 'app1';
}

我导入公共模块时问题出在哪里?!

angular ngfor angular17
1个回答
0
投票

如果您使用

standalone:true
,您需要导入
NgFor
Common
模块
UserListComponent

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