Failed: Template parse errors: 'mat-card-title' is not a known element
两个月前我开始使用 Angular,我设计了一个应用程序并进行了 ng 测试,我遇到了这个错误:
失败:模板解析错误:
'mat-card-title' 不是已知元素:
1. 如果"mat-card-title"是一个 Angular 组件,那么验证它是这个模块的一部分。
2. 如果 'mat-card-title' 是一个 Web 组件,则将 'CUSTOM_ELEMENTS_SCHEMA' 添加到该组件的 '@NgModule.schemas' 以禁止显示此消息。
我不明白我的代码中有什么不起作用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { HeroService } from './hero.service'; import { AngularFireAuthModule } from '@angular/fire/auth'; import { AngularFireModule } from '@angular/fire'; import { AngularFirestoreModule } from '@angular/fire/firestore'; import { AngularFireStorageModule } from '@angular/fire/storage'; import { AngularMaterialModule } from './angular-material/angular-material.module'; import { environment } from 'src/environments/environment'; import { FormsModule } from '@angular/forms'; import { ToastrModule } from 'ngx-toastr'; import { HeroesListComponent } from './heroes-list/heroes-list.component'; import { HeroDetailComponent } from './hero-detail/hero-detail.component'; import { HeroSearchComponent } from './hero-search/hero-search.component'; import { HeroTableComponent } from './hero-table/hero-table.component'; import { LoginComponent } from './login/login.component'; import { NavbarHeroesComponent } from './navbar-heroes/navbar-heroes.component'; import { AddHeroesComponent } from './add-heroes/add-heroes.component'; import { HeroGridComponent } from './hero-grid/hero-grid.component'; import { FlexLayoutModule } from '@angular/flex-layout'; @NgModule({ declarations: [ AppComponent, LoginComponent ], imports: [ BrowserModule, AppRoutingModule, BrowserAnimationsModule, FormsModule, AngularFireModule.initializeApp( environment.firebaseConfig ), AngularFirestoreModule, FlexLayoutModule, AngularFireAuthModule, AngularFireStorageModule, ToastrModule.forRoot(), AngularMaterialModule ], providers: [ HeroService ], bootstrap: [AppComponent] }) export class AppModule { } |
这是 AngularMaterialModule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MatButtonModule, MatTableModule, MatIconModule, MatFormFieldModule, MatSelectModule, MatInputModule, MatRadioModule, MatToolbarModule, MatCardModule, MatGridListModule, MatCheckboxModule, } from '@angular/material'; const moduleAngularMaterial = [ MatButtonModule, MatIconModule, MatFormFieldModule, MatSelectModule, MatInputModule, MatTableModule, MatCheckboxModule, MatRadioModule, MatToolbarModule, MatCardModule, MatGridListModule ]; @NgModule({ declarations: [ ], imports: [ moduleAngularMaterial, CommonModule ], exports: [ moduleAngularMaterial ], }) export class AngularMaterialModule { } |
当组件模块是:
根据您的项目场景,可以选择:
在 Angular 文档中参见:
Angular Material 卡片测试的 API 参考
import {MatCardHarness} from '@angular/material/card/testing';
这可能是你在测试中卡住的原因。
传播数组值
1 | imports: [...moduleAngularMaterial, CommonModule] |