Html ';mat卡';不是Angular 7中的已知元素

Html ';mat卡';不是Angular 7中的已知元素,html,angular,angular-material,Html,Angular,Angular Material,我在这方面看到了很多问题,但似乎不是我遇到的同一个问题。我刚刚创建了我的第二个角度项目。我在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 i

我在这方面看到了很多问题,但似乎不是我遇到的同一个问题。我刚刚创建了我的第二个角度项目。我在
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.component.html
中使用
mat-card
,则不会出现任何错误。
我错过了什么

我在您的声明列表中看不到您的员工组件。EmployeesComponent需要在导入MatCardModule的模块中声明,如:

declarations: [
    EmployeesComponent
],
imports: [
    MatCardModule
]
我猜您可能忘记在应用程序模块中声明EmployeesComponent,或者您有另一个模块,可能是Employees模块,您必须在其中导入MatCardModule

您可以将MatCardModule作为导入

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

假设: 您的组件是EmployeeAddComponent(已包含html(带有mat卡、mat表单字段等))

解决方案: 打开app.module.ts

首先:选中导入“区域”,确保已导入EmployeeAddComponent。 第二:检查声明“区域”中的@NgModule,确保EmployeeAddComponent写在那里。

有时,类型错误“…在Angular 7中不是已知元素”也特定于Visual Studio代码。如果您已经尝试了以下步骤:

  • npm安装——节省角度/材料
  • ng添加@角度/材质

  • 在Visual Studio editor中仍然会收到上述错误消息,然后在编辑器中关闭项目并重新打开。

    首先,您应该在模块的声明中声明employeesComponent。谢谢。我在app-routing.module.ts中有EmployeesComponent。我做了一些改变,现在一切都按预期进行。
    import { MatCardModule } from '@angular/material/card';