angular - 'mat-label' 不是最新 Angular Material 中的已知元素错误

标签 angular typescript angular-material

我的 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,其中包含一些常用小部件(我自己的),您会看到我正在导入 MatFormFieldModuleMatInputModule

// 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/

相关文章:

html - 在鼠标悬停时展开 Material 扩展面板

android - 将 TypeScript 与 HTML5 iOS 应用程序的专有属性结合使用

css - 带 Angular Material 的垂直图标栏

angular - 为什么 mat-tables matRowDef 包含 2 个变量?

angular - 将值加载到垫选择组件

javascript - 输入元素的 Angular 2 焦点和模糊

properties - 属性更改时重新构建/重新渲染 Angular2 组件

Angular TestBed 因 Webpack、Karma/Jasmine 而失败

angular - 如何使用 Testing-Library、Jest 和 JSDOM 测试自定义元素/Web 组件?

validation - Angular2 模板驱动的异步验证器