typescript 角形材料组件不工作:“mat-option”不是已知元素

v1l68za4  于 2023-01-03  发布在  TypeScript
关注(0)|答案(6)|浏览(207)

我正在尝试添加角形材料组件。但是组件无法正常工作。该错误显示

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

jq6vz3qz1#

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

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

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

vmdwslir2#

您需要导入**MatSelectModule**,

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

ee7vknir3#

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

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

否则,在设置mat-optionvalue时可能会遇到问题。

ecfdbz9o

ecfdbz9o4#

我很惊讶用户给出的答案没有一个是正确的,我怎么知道的?是因为我自己也面临着同样的问题。
解决方法是从“@angular/material/select”导入import {MatSelectModule};
并写入导入MatSelectModule

dpiehjr4

dpiehjr45#

您需要在AppModule.ts中导入MatSelectModule

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

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

wlwcrazw6#

大多数人可能会创建自定义组件,然后将其添加到**“shared.module.ts”在这种情况下,您应该注意需要导入到自定义组件本身的内容**

自定义选择模块ts

import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { MatInputModule } from '@angular/material/input';
import { CustomSelectComponent } from './custom-select.component';
import { MatSelectModule } from '@angular/material/select';

@NgModule({
  declarations: [CustomSelectComponent],
  imports: [CommonModule, MatSelectModule, FormsModule],
  exports: [CustomSelectComponent, MatInputModule],
})
export class CustomSelectModule {}

共享.模块.ts

import { NgModule } from '@angular/core';
import { CustomSelectModule } from './components/customSelect/custom-select.module';
import { DatePipe } from './pipes/date.pipe';

@NgModule({
  declarations: [
    ...
  ]
  imports: [
    ...
    
    M̶a̶t̶S̶e̶l̶e̶c̶t̶M̶o̶d̶u̶l̶e̶,̶,
    CustomSelectModule,
    ...
  ],
  exports: [
    ...
    M̶a̶t̶S̶e̶l̶e̶c̶t̶M̶o̶d̶u̶l̶e̶,
    CustomSelectModule,
    ...
  ],
})
export class SharedModule {}

相关问题