首页 > 解决方案 > 'NgIf' 既不是 'ComponentType' 也不是 'DirectiveType'

问题描述

在一个非常简单的组件中使用 *ngIf 时,我得到标题中的错误。例如,下面的代码和 app.module 会导致:

app.component.html:

<ng-container *ngIf="true">
  <div>True</div>
</ng-container>

app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
],
  imports: [
    BrowserModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我对可能导致这种情况的原因感到非常困惑。*ngFor 指令有同样的问题。项目的其余部分直接来自ng new生成的文件。

"@angular/core": "~11.2.8"

标签: angularangular-directive

解决方案


损坏或其他奇怪node_modules的东西很可能是问题的根源。

运行npm ci可以解决问题。


推荐阅读