'mat-label' 不是最新 Angular Material 中的已知元素错误 [英] 'mat-label' is not a known element Error in latest Angular Material

查看:52
本文介绍了'mat-label' 不是最新 Angular Material 中的已知元素错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Angular Material 出现错误:

compiler.js:466 未捕获的错误:模板解析错误:'mat-label' 不是已知元素:1. 如果 'mat-label' 是一个 Angular 组件,那么验证它是否是这个模块的一部分.2. 如果'mat-label' 是一个Web 组件,则将'CUSTOM_ELEMENTS_SCHEMA' 添加到该组件的'@NgModule.schemas' 以抑制此消息.("</mat-form-field><mat-form-field>[ERROR ->]<mat-label>标签和占位符</mat-label><input matInput placeholder="Simple"):

问题:

材料标签位于 MatFormFieldModule 这是链接

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

这是 HTML

<块引用>

<mat-label>标签和占位符</mat-label><input matInput placeholder="简单占位符"></mat-form-field>

解决方案

如果您有多个模块,请确保在每个模块中导入 MatFormFieldModule.仅在根模块中导入它是不够的.

例如,我有一个 CommonWidgetsModule,其中包含一些常见的小部件(我自己的),您会看到我正在导入 MatFormFieldModuleMatInputModule

//common-widgets.module.ts从'@angular/material/form-field' 导入 { MatFormFieldModule };从'@angular/material/input' 导入 { MatInputModule };@NgModule({进口:[通用模块,共享模块,路由器模块,MatFormField 模块,垫输入模块,//导入其他 MatModules...],声明:声明,出口:申报})导出类 CommonWidgetsModule { }

I got an error in my 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"): 

Question:

Material Label is under MatFormFieldModule Here's the link

Now, what is the possible cause of the issue why Mat-Label is unknown to Angular Material.

Here is the HTML

<mat-form-field>
          <mat-label>Both a label and a placeholder</mat-label>
          <input matInput placeholder="Simple placeholder">
</mat-form-field>

解决方案

If you have multiple modules make sure you're importing the MatFormFieldModule in every module. It's not sufficient to just import it in the root module.

For example, I have a CommonWidgetsModule which contains some common widgets (my own) and you'll see I'm importing MatFormFieldModule and 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 { }

这篇关于'mat-label' 不是最新 Angular Material 中的已知元素错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆