无法绑定到“ ngForOf”,因为它不是Angular 9中的“ tr”的已知属性

时间:2020-03-04 19:43:03

标签: angular typescript angular9

我遇到ngFor无法在我的应用程序中工作的问题。

我将我的应用拆分为单独的模块,并将import { CommonModule } from '@angular/common';包含在子模块中,并将import { BrowserModule } from '@angular/platform-browser';包含在我的app.modules.ts文件中,但是在出现错误后仍然出现错误。

Can't bind to 'ngForOf' since it isn't a known property of 'tr'.

我曾尝试研究其他问题,但所有这些问题都包括我自己的CommonModule。

这些是我的文件:

crud-list.component.html

<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>Id</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor='let item of cruds'>
      <td>{{item.OrderNumber}}</td>
    </tr>
  </tbody>
</table>

crud-list.component.ts

import { Component, OnInit } from '@angular/core';
import { CrudRequestService } from '@modules/crud/crud-services/crud-request.service';

@Component({
  selector: 'app-crud-list',
  templateUrl: './crud-list.component.html',
  styleUrls: ['./crud-list.component.scss']
})
export class CrudListComponent {
  public cruds: Array<any>;

  constructor(private objService: CrudRequestService) {
    this.objService.get().subscribe(
      (oDataResult: any) => { this.cruds = oDataResult.value; },
      error => console.error(error)
    );
  }
}

crud.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';

import { CrudListComponent } from '@modules/crud/crud-list/crud-list.component';
import { CrudFormComponent } from '@modules/crud/crud-form/crud-form.component';
import { CrudComponent } from '@modules/crud/crud.component';

const routes: Routes = [
  {
    path: '', component: CrudComponent, children: [
      { path: '', component: CrudListComponent },
      { path: 'create', component: CrudFormComponent },
      { path: 'edit/:id', component: CrudFormComponent }
    ]
  },
];

@NgModule({
  imports: [CommonModule, RouterModule.forChild(routes)],
  declarations: [CrudComponent]
})

export class CrudModule { }

app.module.ts

/* all the imports */

@NgModule({
  declarations: [
    AppComponent,
    ForbidenAccessComponent,
    PageNotFoundComponent,
    AppHeaderComponent,
    AppFooterComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    NgbModule,
    AppRoutingModule,
    CoreModule
  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: JwtInterceptor,
      multi: true,
    },
    BreadcrumbsService,
    AccordionService,
    ModalService,
    RequestService,
    IdentityProviderService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

/* imports */

const routes: Routes = [
  { path: 'home', canActivate: [AuthGuard], component: HomeComponent },
  { path: 'crud', canActivate: [AuthGuard], loadChildren: () => import('@modules/crud/crud.module').then(m => m.CrudModule)},
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  // The error status pages
  { path: '403', component: ForbidenAccessComponent },
  { path: '404', component: PageNotFoundComponent },
  { path: '**', redirectTo: '/404' }
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(routes)
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

6 个答案:

答案 0 :(得分:5)

当我以堆叠式方式重新创建您的问题时,我没有问题。

https://stackblitz.com/edit/angular-60533597

确保将组件添加到模块declarationsRoutes中。

答案 1 :(得分:2)

发现了问题。尽管该应用程序运行时没有任何其他问题,但是当我在StackBlitz上复制问题时,那里的代码给了我一个错误,告诉我我需要在我的@NgModule声明中添加CrudListComponent ,所以我要做的就是在该处添加组件,重新构建应用程序,然后它就起作用了。

crud.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';

import { CrudListComponent } from '@modules/crud/crud-list/crud-list.component';
import { CrudFormComponent } from '@modules/crud/crud-form/crud-form.component';
import { CrudComponent } from '@modules/crud/crud.component';

const routes: Routes = [
  {
    path: '', component: CrudComponent, children: [
      { path: '', component: CrudListComponent },
      { path: 'create', component: CrudFormComponent },
      { path: 'edit/:id', component: CrudFormComponent }
    ]
  },
];

@NgModule({
  declarations: [CrudComponent, CrudListComponent, CrudFormComponent],
  imports: [CommonModule, RouterModule.forChild(routes)]
})

export class CrudModule { }

答案 2 :(得分:1)

crud.module.ts

//crud module should export crud component
@NgModule({
  imports: [CommonModule, RouterModule.forChild(routes)],
  declarations: [CrudComponent],
  exports: [CrudComponent]
})

export class CrudModule { }

您可能还缺少在CommonModule的{​​{1}}数组中添加AppModule的地方。

希望这可以解决您的问题!

答案 3 :(得分:0)

在您的app.module导入中NgbModule是什么?因为我知道@NgModule,但这不需要在导入中:[...]只需在顶部导入。

和一些建议: 在crud-list.component.ts中,您需要取消订阅,并且构造函数仅用于注入,请使用ngOnInit;)

export class CrudListComponent implements OnInit, OnDestroy {
 cruds: Array<any>
 dataSubscription: Subscription

 constructor(private objService: CrudRequestService) { }
 ngOnInit() {
  this.dataSubscription = 
  this.objService.get().subscribe(data => {
   this.cruds = data.value
  })
 }
  ngOnDestroy() {
    this.dataSubscription.unsubscribe()
  }
}

答案 4 :(得分:0)

这个问题存在于angular 9中。我使用的是 angular(9.0.1)版本,因此已升级到〜10.0.9 ,现在可以正常工作。

>

答案 5 :(得分:0)

我在 Ionic 5 Angular 9 中遇到了相同的问题,并搜索了数小时的解决方案。基本上,我确定了 3个主要原因

1。清理并重建(或重新安装)您的项目。 离子5示例:项目目录中的cordova clean; ionic cordova build android(或rm -rf node_modules; npm install)。

2。通常,这是由于 CommonModule 未包含在组件模块中(或 BrowserModule 未包含在应用程序模块中)引起的)。离子5示例:

list.module.ts

import { CommonModule } from '@angular/common';
import { ListPage } from './list';
...

@NgModule({
  imports: [
    CommonModule,
    ...
  ],
  declarations: [ListPage]
})
export class ListModule {}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
...

@NgModule({
  exports: [
    FormsModule,
    ReactiveFormsModule
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    ...
  ],
  declarations: [AppComponent],
  providers: [
    StatusBar,
    ...
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

3。。根据运行的Angular版本(如果运行的是Ivy)(如果当前使用的是Ionic 5,则可能不是),则需要具有 ModalComponent 父模块 entryComponents 以及声明中。在下面的示例中,CalendarModule将ListComponent显示为模态。离子5示例:

calendar.module.ts

import { CommonModule } from '@angular/common';
...
import { CalendarPage } from './calendar';
import { ListPage } from "../../modals/list/list";

@NgModule({
  imports: [
    CommonModule,
    ...
  ],
  declarations: [CalendarPage, ListPage],
  entryComponents: [ListPage],
  providers: []
})
export class CalendarModule {}

P.S .:要取消和激活 Ivy ,请在

的TypeScript配置中设置Angular编译器选项

tsconfig.json

"angularCompilerOptions": {
    ...
    "enableIvy": false
 },