简体   繁体   English

Angular mat-form-field/mat-label 问题

[英]Angular Problem with mat-form-field/mat-label

I started learning Angular Materials, and I have a problem with adding example from their site.我开始学习 Angular 材料,但我在从他们的站点添加示例时遇到了问题。

To my html file I added:我在 html 文件中添加了:

        <mat-form-field class="example-form-field">
          <mat-label>Clearable input</mat-label>
          <input matInput type="text" [(ngModel)]="value">
          <button *ngIf="value" matSuffix mat-icon-button aria-label="Clear" (click)="value=''">
            <mat-icon>close</mat-icon>
          </button>
        </mat-form-field>

And I have got few errors: 'mat-form-field' is not a known element:而且我几乎没有错误:'mat-form-field' 不是已知元素:

  1. If 'mat-form-field' is an Angular component, then verify that it is part of this module.如果“mat-form-field”是一个 Angular 组件,则验证它是该模块的一部分。
  2. If 'mat-form-field' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress如果“mat-form-field”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制

My app.module.ts file:我的 app.module.ts 文件:

import {MatInputModule} from '@angular/material/input';
import {Component} from '@angular/core';
import { NgModule} from '@angular/core';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { BrowserModule } from '@angular/platform-browser';
import { MatRippleModule } from '@angular/material/core';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon'
import {MatButtonModule} from '@angular/material/button';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';

@NgModule ({

  schemas: [CUSTOM_ELEMENTS_SCHEMA],

  imports: [
    MatSlideToggleModule,
    BrowserModule,
    FormsModule,
    MatFormFieldModule,
    MatButtonModule,
    MatIconModule,
    MatInputModule,
    MatRippleModule
  ]
})
class AppModule {}
@Component({
  selector: 'input-clearable-example',
  templateUrl: './okno_testowe.html',
  styleUrls: ['./log.css'],
})
export class InputClearableExample {
  value = 'Clear me';
}

and css:和 css:

.example-form-field {
    width: 200px;
  }

What could be the problem here?这里可能是什么问题? I assume I made some simple mistake because I am just taking my first steps with these technologies我认为我犯了一些简单的错误,因为我才刚刚迈出这些技术的第一步

  • You need to bootstrap the InputClearableExample component, this will be the component that will render the initial content of the app您需要引导 InputClearableExample 组件,这将是呈现应用程序初始内容的组件
  • Add the InputClearableExample component in the declarations array在声明数组中添加 InputClearableExample 组件
  • schemas: [CUSTOM_ELEMENTS_SCHEMA], is not required模式:[CUSTOM_ELEMENTS_SCHEMA],不是必需的
  • Also add BrowserAnimationsModule, Angular material requires this module另外添加BrowserAnimationsModule,Angular素材需要这个模块
@NgModule ({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    MatFormFieldModule,
    MatButtonModule,
    MatIconModule,
    MatInputModule,
    MatRippleModule
  ],
  declarations: [InputClearableExample],
  bootstrap: [InputClearableExample],
})
export class AppModule {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM