【问题标题】:Error Angular 13: ngForOf not used by any directive... Can't bind to 'ngForOf'错误 Angular 13:ngForOf 未被任何指令使用...无法绑定到“ngForOf”
【发布时间】:2022-01-22 06:58:23
【问题描述】:

我收到了这个错误

Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". > 

无法绑定到“ngForOf”,因为它不是“a”的已知属性

我已经导入了所有必要的模块:CommonModuleBrowserModuleReactiveFormsModule

app.module.ts:

import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { RecipesComponent } from './components/recipes/recipes.component';
import { RecipeListComponent } from './components/recipes/recipe-list/recipe-list.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    RecipesComponent,
    RecipeListComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    ReactiveFormsModule,
    CommonModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule { }

recipe-list.component.ts :

<div class="row">
  <div class="col-xs-12">
    <a href="#" class="list-group-item clearfix" *ngFor="let recipe of recipes"> // error ngFor
      <div class="pull-left">
        <h4 class="list-group-item-heading"> {{ recipe.name}} </h4>
      </div>
      <span class="pull-right">
        <img [src]="recipe.imagePath" alt=" {{recipe.name}} " srcset="" class="img-responsive" style="max-height: 50px">
      </span>
    </a>
    <app-recipe-item></app-recipe-item>
  </div>
</div>

我正在使用 Angular v13.1.1 / Node v16.13.1 / VSCode v1.63.2

【问题讨论】:

  • 顺便说一句,项目工作正常,但错误仍然出现
  • 删除 schemas 作为 ngModule 的道具。它会导致错误。
  • 您不需要架构:[CUSTOM_ELEMENTS_SCHEMA]
  • 错误仍然出现,我删除了架构并卸载了VS代码并重新安装但没有结果!

标签: angular typescript visual-studio-code angular13


【解决方案1】:

禁用 Visual Studio 扩展“Angular 语言服务”解决了这个问题。

【讨论】:

  • 非常感谢伙计,我很困惑为什么突然出现这个错误。我什至开始了一个新的 Angular 项目并遇到了同样的问题。
  • 不客气!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2020-06-17
  • 2020-10-25
  • 2018-12-07
  • 1970-01-01
  • 2020-10-13
  • 2020-11-12
相关资源
最近更新 更多