角材料组件不起作用:'mat-option'不是一个已知元素

时间:2018-08-28 08:00:55

标签: angular typescript angular-material angular6

我正在尝试添加角形材料成分。但该组件无法正常工作。该错误说

Uncaught Error: Template parse errors: 'mat-option' is not a known element:
// ...

我认为错误来自app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { SidebarComponent } from './sidebar/sidebar.component';
import { PostsComponent } from './posts/posts.component';
import { UsersComponent } from './users/users.component';
import { DetailsComponent } from './details/details.component';
import { HttpClientModule } from '@angular/common/http';
import { FooterComponent } from './footer/footer.component'; 
import {MatButtonModule, MatCheckboxModule} from '@angular/material';
import {MatFormFieldModule} from '@angular/material/form-field';
import { MyFormComponent } from './my-form/my-form.component';
@NgModule({
  declarations: [
    AppComponent,
    SidebarComponent,
    PostsComponent,
    UsersComponent,
    DetailsComponent,
    FooterComponent, MyFormComponent
  ],
  imports: [
    BrowserModule, AppRoutingModule,HttpClientModule,MatButtonModule, MatCheckboxModule,MatFormFieldModule
  ]
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

5 个答案:

答案 0 :(得分:6)

您需要导入MatSelectModule,因为mat-option在此模块中声明,并将其添加到AppModule导入中

import { ..., MatSelectModule, ... } from '@angular/material';

@NgModule({
   imports: [ ..., MatSelectModule, ...]
})
export class AppModule { }

答案 1 :(得分:0)

您需要导入 MatSelectModule

imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MaterialModule,
    MatSelectModule,
    //..Other modules here
],

答案 2 :(得分:0)

您需要在AppModule.ts中导入 MatSelectModule

import {MatButtonModule, MatCheckboxModule, MatSelectModule} from '@angular/material';

imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    MatButtonModule,
    MatCheckboxModule,
    MatFormFieldModule,
    MatSelectModule
  ]

答案 3 :(得分:0)

实际上您还需要添加 MatOptionModule 而不仅仅是 MatSelectModule

imports: [
  ...
  MatSelectModule,
  MatOptionModule,
  ...
],

否则在设置 valuemat-option 时可能会遇到问题。

答案 4 :(得分:0)

我很惊讶用户给出的答案都不正确,我是怎么知道的? 是因为我自己也遇到了同样的问题。

解决办法是导入 从'@angular/material/select'导入{MatSelectModule};

并在 import 中写入 垫选择模块