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

当前回答

需要记住的事情:

当使用自定义模块(除AppModule之外的模块)时,需要导入其中的公共模块。

yourmodule.module.ts

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

@NgModule({
  imports: [
    CommonModule
  ],
  exports:[ ],
  declarations: []
})

其他回答

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

feature.routing.module.ts:

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

feature.module.ts:

     imports: [ FeatureRoutingModule ],
     declarations: [],

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

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

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

@NgModule({
  imports: [
    CommonModule
]})

它将解决你的错误。

自定义模块需要通用模块

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


@NgModule({
  imports: [
    CommonModule
  ]
})

未来的读者

检查以下每一项:

该组件是在SINGLE angular模块中声明的 确保你有import {CommonModule} from '@angular/common'; 重新启动IDE/编辑器 重新启动开发服务器(ng serve)

我认为我的情况是一个罕见的情况,但我还是会回答。

我没有任何语法错误,我导入了BrowserModule,我重新声明了我的IDE (VS Code),但我仍然在我的模板中有红线,因为我使用了*ngFor。

这是因为我正在使用一个扩展,Angular语言服务,但它相当老了。更新后,错误停止出现。