模板解析错误:'md-form-field'不是已知元素

Suv*_*kar 40 angular-material angular-material2 angular

我正在使用Angular 4和Angular Material 2.对于以下代码:

<form>
  <md-form-field>
    <input mdInput [ngModel]="userName" placeholder="User" [formControl]="usernameFormControl">
    <md-error *ngIf="usernameFormControl.hasError('required')">
      This is <strong>required</strong>
    </md-error>
    <input mdInput [ngModel]="password" placeholder="Password" [formControl]="passwordFormControl">
    <md-error *ngIf="passwordFormControl.hasError('required')">
      This is <strong>required</strong>
    </md-error>
    <button md-raised-button color="primary" [disabled]="!usernameFormControl.valid || !passwordFormControl.valid">Login</button>
  </md-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

模板解析错误:'md-form-field'不是已知元素:1.如果'md-form-field'是Angular组件,则验证它是否是此模块的一部分.2.如果'md-form-field'是Web组件,则将'CUSTOM_ELEMENTS_SCHEMA'添加到此组件的'@NgModule.schemas'以禁止显示此消息.("[错误 - >]

你能不能帮我解决我失踪的地方?

以下是我app.module.ts导入材料模块的代码:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { LoginComponent } from './login.component';

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import {
  MdAutocompleteModule,
  MdButtonModule,
  MdButtonToggleModule,
  MdCardModule,
  MdCheckboxModule,
  MdChipsModule,
  MdCoreModule,
  MdDatepickerModule,
  MdDialogModule,
  MdExpansionModule,
  MdGridListModule,
  MdIconModule,
  MdInputModule,
  MdListModule,
  MdMenuModule,
  MdNativeDateModule,
  MdPaginatorModule,
  MdProgressBarModule,
  MdProgressSpinnerModule,
  MdRadioModule,
  MdRippleModule,
  MdSelectModule,
  MdSidenavModule,
  MdSliderModule,
  MdSlideToggleModule,
  MdSnackBarModule,
  MdSortModule,
  MdTableModule,
  MdTabsModule,
  MdToolbarModule,
  MdTooltipModule
} from '@angular/material';

import {CdkTableModule} from '@angular/cdk';

@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpModule,
    FormsModule,
    ReactiveFormsModule,
    MdAutocompleteModule,
    MdButtonModule,
    MdButtonToggleModule,
    MdCardModule,
    MdCheckboxModule,
    MdChipsModule,
    MdCoreModule,
    MdDatepickerModule,
    MdDialogModule,
    MdExpansionModule,
    MdGridListModule,
    MdIconModule,
    MdInputModule,
    MdListModule,
    MdMenuModule,
    MdNativeDateModule,
    MdPaginatorModule,
    MdProgressBarModule,
    MdProgressSpinnerModule,
    MdRadioModule,
    MdRippleModule,
    MdSelectModule,
    MdSidenavModule,
    MdSliderModule,
    MdSlideToggleModule,
    MdSnackBarModule,
    MdSortModule,
    MdTableModule,
    MdTabsModule,
    MdToolbarModule,
    MdTooltipModule,
    CdkTableModule
  ],
  declarations: [
    AppComponent,
    LoginComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

Fai*_*sal 58

更新:

因为2.0.0-beta.12,md前缀已被删除,有利于mat前缀.有关详细信息,请参阅此CHANGELOG:

所有"md"前缀都已删除.有关详细信息,请参阅beta.11注释中的弃用声明.

更新后,<md-form-field>应更改为<mat-form-field>.此外,MdFormFieldModuleMdInputModule应改为MatFormFieldModuleMatInputModule:

import { MatFormFieldModule } from '@angular/material';
import { MatInputModule } from '@angular/material';

@NgModule({
  imports: [
    ....
    MatFormFieldModule,
    MatInputModule,
    ....
  ]
Run Code Online (Sandbox Code Playgroud)

这是使用更新StackBlitz演示的链接2.0.0-beta.12.


原版的:

<md-form-field>是在2.0.0-beta.10中引入的.请参阅以下changelog文档:

md-input-container重命名为md-form-field(同时仍然向后兼容).旧选择器将在后续版本中删除.

这是完成CHANGELOG的链接.

要使用<md-form-field>选择器,请确保安装了2.0.0-beta.10版本的材料.而且,您需要在导入中导入MdFormFieldModule模块AppModule:

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

@NgModule({
  imports: [
    ....
    MdFormFieldModule,
    MdInputModule,
    ....
  ]
Run Code Online (Sandbox Code Playgroud)

对于偶然发现这个问题的人来说,这里是StackBlitz 上工作演示的链接.

  • 嘿@Faisal感谢您的回复 - 似乎也是我所寻找的.虽然使用`2.0.0-beta.10`我得到错误:`node_modules/@ angular/material/material"'没有导出成员'MdFormFieldModule'.感谢adnvace中的任何想法! (2认同)