我使用的是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>

当前回答

当使用"app-routing. "我们忘记导入CommonModule了。记得导入!

import { CommonModule } from "@angular/common";
@NgModule({  imports: [ CommonModule]})

其他回答

我收到了错误,因为我正在使用的组件没有在模块的declarations:[]部分中注册。

添加组件后,错误消失了。我本希望看到比这条错误消息更清晰的信息来指出真正的问题。

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

feature.routing.module.ts:

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

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

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

当ngFor语法没有正确编写时,这个错误也会被抛出,在我的例子中,我有:

<ng-template *ngForOf="let key of SomeObject; index as i">

它通过使用以下语法得到了修复:

<ng-template ngFor let-key [ngForOf]="SomeObject" let-i="index">

如果您有模块实现,则必须导入组件并在声明中设置组件

祝你编程好运:)

在我的例子中,这个组件没有在app.module.ts中列出。