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

当前回答

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

feature.routing.module.ts:

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

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

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

其他回答

所以请确保

指令中没有语法错误 浏览器(在应用程序模块)和公共(在其他/子)模块被导入(与上面提到的Günter Zöchbauer相同) 如果你在应用程序中有路由,那么路由模块应该导入到应用程序模块中 所有路由组件的模块也被导入到App模块中,例如: app-routing.module。Ts为: const routes: routes = [ {path: ",组件:CustomerComponent}, {path: 'admin',组件:AdminComponent} ];

然后App模块必须导入@NgModule()中的CustomerComponent和AdminComponent模块。

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

feature.routing.module.ts:

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

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

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

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

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

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

自定义模块需要通用模块

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


@NgModule({
  imports: [
    CommonModule
  ]
})