例如,在 app.module.ts 文件中导入以下模块:
import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select';
然后在 @NgModule 的 imports 数组中加入这些模块:
@NgModule({ imports: [ BrowserModule, HttpClientModule, BrowserAnimationsModule, MatInputModule, MatSelectModule, // ... ], // ... })
例如,如果您在组件中需要使用 mat-select 组件,则需要将其导入:
import { MatSelect } from '@angular/material/select';
然后在组件类中使用:
@Component({ // ... }) export class MyComponent { @ViewChild(MatSelect) select: MatSelect; // ... })
import { MatNativeDateModule, MAT_DATE_LOCALE } from '@angular/material/core'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { MatTooltipModule } from '@angular/material/tooltip'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
然后在 @NgModule 的 providers 数组中加入这些选项:
@NgModule({ imports: [ BrowserModule, HttpClientModule, BrowserAnimationsModule, // ... ], providers: [ { provide: MAT_DATE_LOCALE, useValue: 'zh-CN' }, { provide: MAT_FORM_FIELD_DEFAULT_OPTIONS, useValue: { floatLabel: 'always' } } ], // ... })