'mat-card' 不是 Angular 7 中的已知元素

新手上路,请多包涵

我已经看到很多关于此的问题,但似乎与我遇到的问题不同。我刚刚创建了我的第二个角度项目。我在 src/app/employees 下有一个新组件,我试图在其中使用 employees.component.html 。我得到的错误是:

 Uncaught Error: Template parse errors:
'mat-card' is not a known element:
1. If 'mat-card' is an Angular component, then verify that it is part of this module.
2. If 'mat-card' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<mat-card> </mat-card>

现在,在 app.module.ts 我有:

 import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { LOCALE_ID, NgModule } from "@angular/core";
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import {
  MatButtonModule,
  MatFormFieldModule,
  MatIconModule,
  MatInputModule,
  MatListModule,
  MatSelectModule,
  MatSidenavModule,
  MatCardModule,
  MatTableModule
} from "@angular/material";

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    HttpClientModule,
    MatButtonModule,
    MatFormFieldModule,
    MatIconModule,
    MatListModule,
    MatInputModule,
    MatSelectModule,
    MatSidenavModule,
    MatCardModule,
    MatTableModule
  ],....
})
export class AppModule {}

如果我在 app.component.html mat-card 则不会出现任何错误。我在这里错过了什么?

原文由 Woody 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 520
2 个回答

我在您的声明列表中看不到您的 EmployeesComponent。 EmployeesComponent 需要在导入 MatCardModule 的同一模块中声明,例如:

 declarations: [
    EmployeesComponent
],
imports: [
    MatCardModule
]

我猜测要么您忘记在您的应用程序模块中声明 EmployeesComponent,要么您有另一个模块,也许是 Employees 模块,您必须在其中导入 MatCardModule。

您可以将 MatCardModule 导入为

import { MatCardModule } from '@angular/material/card';

原文由 SnorreDan 发布,翻译遵循 CC BY-SA 4.0 许可协议

在您的 app.module.ts 文件中添加以下行:

 import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

schemas: [CUSTOM_ELEMENTS_SCHEMA],

原文由 Trupti 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题