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

当前回答

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

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

其他回答

我有一个问题,因为**代替*

*ngFor="let ingredient of ingredients"

**ngFor="let ingredient of ingredients"

对于Angular 10:

将BrowserModule添加到routes模块的导入中。 确保你在app模块声明中添加了不工作的组件。

如果不执行第2步,将触发此错误!

请务必重新启动ng服务!!

我遇到过类似的错误(*ngIf),即使我所有的导入都是OK的,并且组件呈现时没有任何其他错误+路由是OK的。

在我的例子中,AppModule没有包含这个特定的模块。奇怪的是,它并没有抱怨这一点,但这可能与Ivy使用ng serve的方式有关(类似于根据路由加载模块,但不考虑它的依赖关系)。

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

feature.routing.module.ts:

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

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

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

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

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