首页 文章

md-form-field不是已知的Angular Material元素

提问于
浏览
1

错误

Uncaught Error: Template parse errors:
'md-form-field' is not a known element:
 1. If 'md-form-field' is an Angular component, then verify that it is part 
of this module.
2. If 'md-form-field' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' 
to the '@NgModule.schemas' of this component to suppress this message. ("

我用角度材料实现了角度输入

网址https://material.angular.io/components/input/overview

HTML

<form class="example-form">
      <md-form-field class="example-full-width">
     <input mdInput placeholder="Favorite food" value="Sushi">
    </md-form-field>
    </form>

谷歌搜索后我找到了链接

Angular Material2:'md-form-field'不是已知元素

这说

import { MdFormFieldModule } from '@angular/material';

  @NgModule({
  imports: [
  ....
  MdFormFieldModule,
   ....
 ]

导入后的app.module.ts

从'@ angular / material'导入;

ERROR in D:/frontend/src/app/app.module.ts (97,10): Module '"D:/frontendapp/node_modules/@angular/material/material"' has no exported member 'MdFormFieldModule'.
  • 任何人都可以建议我失踪的东西 .

3 回答

  • 0

    添加 schemas: [CUSTOM_ELEMENTS_SCHEMA]import {MdInputModule} from '@angular/material' 而不是 MdFormFieldModule 它已经过时了 .

    @NgModule({
        declarations: [
            ..
        ],
        imports: [
            MdInputModule,
            ..
        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
    

    到你的模块

  • 0

    如果您在更新版本的Angular中收到该错误,我发现AngularMaterial indicates它已在最近版本中将组件标记(无论您想要调用它)从 md-form-field 更改为 mat-form-field . 请注意 mat-form-field 中的 mat . 我从版本2.0.0-beta.3更新到版本5.2.5 . This breaking change appears to have come in 2.0.0-beta.12.

    虽然我需要在 app.module.ts 文件中导入 import { MatInputModule } from '@angular/material'; ,但是 .html 文件中的组件标记更改缓解了控制台错误 'md-form-field' is not a known element: .

    注意:我在将 md-input-container 标记升级到 md-form-field 标记时遇到了这个问题,并且我花了很多时间才意识到这些标记已经被更新到了新的 mat-form-field . 无论如何,如果您使用 md-input-containermd-form-field 从旧版本的材质更新为更新版本的材质,我建议您只需更新一直到 mat-form-field ,如果可以的话 .

    注意2:确保查找任何切向更新 - 例如更改指令名称 . 例如,如果您有 mdInput 的任何情况,您可能需要将它们更改为 matInput . 类似的东西 . 那些 mdmat 的更改可能会通过您的代码多产 . 如果您的编辑器具有"replace-all"跨多个文件功能,它将是您最好的朋友 . 除了旧的AngularMaterial世界之外,至少 md 不是一对普通的字母 .

  • 1

    导入此模块:MdInputModule然后使用此代码剪辑并享受:

    <md-input-container>
       <input mdInput placeholder="User">
    </md-input-container>
    

相关问题