【问题标题】:Angular Material component not working: 'mat-option' is not a known elementAngular Material 组件不起作用:'mat-option' 不是已知元素
【发布时间】:2019-02-02 19:02:52
【问题描述】:

我正在尝试添加角度材质组件。但该组件无法正常工作。那个错误说

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 { }

【问题讨论】:

  • 标记答案

标签: angular typescript angular-material angular6


【解决方案1】:

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

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

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

【讨论】:

  • 完成。现在错误说“src/app/app.module.ts(27,5) 中的错误:错误 TS2304:找不到名称‘MaterialModule’。”
  • 在我的代码中我没有MaterialModule - 我有MatSelectModule
  • MatFormField.html:1 ERROR 错误:找到合成属性@transitionMessages。请在您的应用程序中包含“BrowserAnimationsModule”或“NoopAnimationsModule”。这是什么>?
  • 这意味着如果你使用动画你需要添加模块BrowserAnimationsModule
  • 在 Angular 10 中,它是 import { MatSelectModule } from '@angular/material/select';
【解决方案2】:

其实你还需要添加MatOptionModule,而不仅仅是MatSelectModule

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

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

【讨论】:

    【解决方案3】:

    您需要在 AppModule.ts 中导入 MatSelectModule

    import {MatButtonModule, MatCheckboxModule, MatSelectModule} from '@angular/material';
    
    imports: [
        BrowserModule,
        AppRoutingModule,
        HttpClientModule,
        MatButtonModule,
        MatCheckboxModule,
        MatFormFieldModule,
        MatSelectModule
      ]
    

    【讨论】:

      【解决方案4】:

      你需要导入MatSelectModule

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

      【讨论】:

      • [ts] 找不到名称“MaterialModule”。任何
      • 您缺少浏览器和材料的相关模块
      • @user1177842 你为什么删除答案?
      • 我没有删除
      • 我认为它已被删除:P
      【解决方案5】:

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

      解决方案是导入 从“@angular/material/select”导入 {MatSelectModule};

      并写入导入 MatSelectModule

      【讨论】:

        猜你喜欢
        • 2020-07-02
        • 1970-01-01
        • 1970-01-01
        • 2019-06-27
        • 1970-01-01
        • 2018-05-17
        • 2019-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多