angulartypescriptkarma-jasmineangular-material-8

Failed: Template parse errors: 'mat-card-title' is not a known element


I started angular two months ago, I designed an application and doing the ng test I have this error:

Failed: Template parse errors: 'mat-card-title' is not a known element: 1. If 'mat-card-title' is an Angular component, then verify that it is part of this module. 2. If 'mat-card-title' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@ NgModule.schemas' of this component to suppress this message. I do not understand what does not work in my code.

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

this is AngularMaterialModule

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


Solution

  • These kind of issues comes when component module is either:

    1. Not imported in the module where it is declared and intended to be used
    2. Not re-exported from a SharedModule which declares the component in which component for Angular is used.

    Depending on the scenario of your project, either:

    1. Import the mat component module in the same module where it is declared and intended to be used, or
    2. Re-export the mat ui component module from [YourCustom]Module, which imports the mat Ui module for Angular.