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

Ran*_*z67 13 angular-material angular

我的Angular材料中出现错误:

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"): 
Run Code Online (Sandbox Code Playgroud)

题:

材料标签位于MatFormFieldModule 下面是链接

现在,问题的可能原因是什么,为什么Mat-Label不为Angular Material所知.

这是HTML

<mat-form-field>
          <mat-label>Both a label and a placeholder</mat-label>
          <input matInput placeholder="Simple placeholder">
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

Sim*_*ver 12

如果您有多个模块,请确保要MatFormFieldModule在每个模块中导入。仅将其导入到根模块中是不够的。

例如,我有一个CommonWidgetsModule,其中包含一些常见的小部件(我自己的),但是我只将其导入MatFormFieldModuleapp.module.ts文件中。

// common-widgets.module.ts

@NgModule({
  imports: [
    CommonModule,
    SharedModule,
    RouterModule,

    MatIconModule,

    MatFormFieldModule,
    MatInputModule
  ],
  declarations: DECLARATIONS,
  exports: DECLARATIONS
})
export class CommonWidgetsModule { }
Run Code Online (Sandbox Code Playgroud)

  • 并且您还需要添加`MatInputModule`以避免出现错误`错误:mat-form-field必须包含MatFormFieldControl。`(https://github.com/angular/material2/issues/7704) (2认同)

Mat*_*fek -3

将@angular/material升级到“5.2.0”,问题就会消失。