我使用的是Angular2 2.1.0。当我想要显示公司列表时,我得到了这个错误。

在file.component.ts中:

public companies: any[] = [
    { "id": 0, "name": "Available" },
    { "id": 1, "name": "Ready" },
    { "id": 2, "name": "Started" }
];

在file.component.html中:

<tbody>
  <tr *ngFor="let item of companies; let i =index">
     <td>{{i}}</td>
     <td>{{item.name}}</td>
  </tr>
</tbody>

当前回答

如果你已经采用了(BrowserModule, CommonModule, FormsModule) 它仍然没有工作

然后,您所要做的就是检查模块中是否声明了有错误的组件

其他回答

我开始在Angular8基础上的live项目得到了上述问题,但当使用“app-routing。我们忘记导入CommonModule了。记得导入!

import { CommonModule } from '@angular/common';

@NgModule({
  imports: [
    CommonModule
]})

它将解决你的错误。

如果您没有在特性模块中声明路由组件,也会发生这种情况。例如:

feature.routing.module.ts:

...
    {
        path: '',
        component: ViewComponent,
    }
...

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

注意ViewComponent不在declarations数组中,而它应该在。

作为将来的参考,我正在Angular 12上做一些单元测试,遇到了这个问题。

我的问题是我正在使用ng bootstrap库并测试一个用NgbModal创建新模态的方法。

我得到这些警告,因为我忘记导入弹出窗口所创建的组件。

我不需要导入FormsModule、CommonModule或BrowserModule。

因此,如果您遇到这些错误,请确保您试图创建实例的组件在导入中列出。

以我为例,问题是我的队友在模板中提到了*ngfor而不是*ngfor。奇怪的是,没有正确的错误来处理这个问题(在Angular 4中)。

在所有代码中使用正确的语法后,请查看是否在angular模块的声明中提到了你的组件。 如下所示:

@NgModule ({ 声明:[ AppComponent, YourComponent ),