我的 Angular Material 出现错误:
compiler.js:466 Uncaught Error: Template parse errors:
'mat-label' is not a known element:
1. If 'mat-label' is an Angular component, then verify that it is part of this module.
2. If 'mat-label' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
</mat-form-field>
<mat-form-field>
[ERROR ->]<mat-label>Both a label and a placeholder</mat-label>
<input matInput placeholder="Simple"):
问题:
Material 标签在 MatFormFieldModule 下 Here's the link
现在,导致 Angular Material 无法识别 Mat-Label 的问题的可能原因是什么。
这是HTML
<mat-form-field> <mat-label>Both a label and a placeholder</mat-label> <input matInput placeholder="Simple placeholder"> </mat-form-field>
最佳答案
如果您有多个模块,请确保在每个模块中导入 MatFormFieldModule
。仅仅将它导入到根模块中是不够的。
例如,我有一个 CommonWidgetsModule,其中包含一些常用小部件(我自己的),您会看到我正在导入 MatFormFieldModule
和 MatInputModule
// common-widgets.module.ts
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
@NgModule({
imports: [
CommonModule,
SharedModule,
RouterModule,
MatFormFieldModule,
MatInputModule,
// import other MatModules...
],
declarations: DECLARATIONS,
exports: DECLARATIONS
})
export class CommonWidgetsModule { }
关于angular - 'mat-label' 不是最新 Angular Material 中的已知元素错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873128/