[英]*ngFor Can't bind to 'ngForOf' since it isn't a known property of 'div'.'
I have been using NGFor for a long time, but it seems that suddenly in this new application I am getting我已经使用 NGFor 很长时间了,但似乎突然在这个新应用程序中我得到了
Can't bind to 'ngForOf' since it isn't a known property of 'div'.'无法绑定到“ngForOf”,因为它不是“div”的已知属性。
When looking it up online it is showing older version of Angular, which is odd.在线查找时显示的是旧版本的 Angular,这很奇怪。 Likewise It is telling me to add BrowserModule in the imports which is generally a normal thing.同样,它告诉我在导入中添加 BrowserModule,这通常是正常的。 I currently am working on a App with Routing, so the App uses the RouterModule to fetch the right page.我目前正在开发一个带有路由的应用程序,因此该应用程序使用 RouterModule 来获取正确的页面。 In the App module, i have it as an import, along with BrowserModule.在 App 模块中,我将它与 BrowserModule 一起作为导入。
EDIT I ended up doing a *ngFor loop in my base page, so it seems to work.编辑我最终在我的基本页面中做了一个 *ngFor 循环,所以它似乎工作。 I am thinking the issue happens from the concept of the route?我在想问题是从路线的概念上发生的? I access it in my RouterModule by:我通过以下方式在我的 RouterModule 中访问它:
{path: 'industry/manufacturing', component: ManufacturingComponent},
Is accessing by route requiring me to add CommonModule to the RouterModule?通过路由访问是否需要我将 CommonModule 添加到 RouterModule?
My Angular specs are:我的 Angular 规格是:
Angular CLI: 10.0.8
Node: 14.12.0
OS: darwin x64
Angular: 10.0.14
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.1000.8
@angular-devkit/build-angular 0.1000.8
@angular-devkit/build-optimizer 0.1000.8
@angular-devkit/build-webpack 0.1000.8
@angular-devkit/core 10.0.8
@angular-devkit/schematics 10.0.8
@angular/cdk 8.2.3
@angular/cli 10.0.8
@angular/material 8.2.3
@ngtools/webpack 10.0.8
@schematics/angular 10.0.8
@schematics/update 0.1000.8
rxjs 6.5.5
typescript 3.9.7
webpack 4.43.0
<div *ngFor="let mat of materials">
{{mat.name}}: {{mat.quantity}}
</div>
where in the ts code i define it as:在 ts 代码中,我将其定义为:
materials = [{"name": "foo", "quantity": 5}];
My app.module looks like:我的 app.module 看起来像:
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule
]
})
AppRoutingModule also is just a simple: AppRoutingModule 也很简单:
const routes: Routes = [
{path: '', component: MainComponent},
{path: 'industry/manufacturing', component: ManufacturingComponent},
{path: '**', component: PageNotFoundComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
I am not sure what is going on that is out of the ordinary here.我不确定这里发生了什么不寻常的事情。
I think you are missing an import of CommonModule
In your imports, unless its declared some where else that I cant see.我认为您在导入时缺少CommonModule
的导入,除非它在其他我看不到的地方声明。 Edit: Your app module has BrowserModule which includes it, but it doesnt know about your components, Either add CommonModule
to your RoutingModule
(not recommended, its not the router job to handle who has what), or declare the components in your AppModule
to let him know which components uses his imports.编辑:您的应用程序模块有 BrowserModule 包含它,但它不知道您的组件,将CommonModule
添加到您的RoutingModule
(不推荐,它不是处理谁拥有什么的路由器工作),或者在您的AppModule
中声明组件让他知道哪些组件使用他的进口。 Please refer to https://stackblitz.com/angular/qypdxgpopxp?file=src%2Fapp%2Fadmin%2Fadmin.module.ts for a wide example of routing and modules有关路由和模块的广泛示例,请参阅https://stackblitz.com/angular/qypdxgpopxp?file=src%2Fapp%2Fadmin%2Fadmin.module.ts
Try these steps and see.试试这些步骤看看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.