我使用的是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>
我有同样的错误,但我有CommonModule导入。相反,我在不应该的地方留下了逗号,因为在分割模块时需要复制/粘贴:
@NgModule({
declarations: [
ShopComponent,
ShoppingEditComponent
],
imports: [
CommonModule,
FormsModule,
RouterModule.forChild([
{ path: 'shop', component: ShopComponent }, <--- offensive comma
])
]
})
如果您没有在特性模块中声明路由组件,也会发生这种情况。例如:
feature.routing.module.ts:
...
{
path: '',
component: ViewComponent,
}
...
feature.module.ts:
imports: [ FeatureRoutingModule ],
declarations: [],
注意ViewComponent不在declarations数组中,而它应该在。