1

I am trying to use Angular Material in Angular 6 project but getting errors

  1. 'mat-panel-title' is not a known element:
  2. 'mat-panel-description' is not a known element:
  3. 'mat-expansion-panel-header' is not a known element:
  4. 'mat-form-field' is not a known element:
  5. 'mat-expansion-panel' is not a known element:

Here is the code

<mat-accordion>
  <mat-expansion-panel>
    <mat-expansion-panel-header>
      <mat-panel-title>
        Personal data
      </mat-panel-title>
      <mat-panel-description>
        Type your name and age
      </mat-panel-description>
    </mat-expansion-panel-header>

    <mat-form-field>
      <input matInput placeholder="First name">
    </mat-form-field>

    <mat-form-field>
      <input matInput placeholder="Age">
    </mat-form-field>
  </mat-expansion-panel>
  <mat-expansion-panel (opened)="panelOpenState = true"
                       (closed)="panelOpenState = false">
    <mat-expansion-panel-header>
      <mat-panel-title>
        Self aware panel
      </mat-panel-title>
      <mat-panel-description>
        Currently I am {{panelOpenState ? 'open' : 'closed'}}
      </mat-panel-description>
    </mat-expansion-panel-header>
    <p>I'm visible because I am open</p>
  </mat-expansion-panel>
</mat-accordion>

ts file
panelOpenState = false;

Kunal Tyagi
  • 2,341
  • 1
  • 15
  • 26

3 Answers3

4

You have to import the Material Module in the root of your project or in the module if you are using module architecture. Check this https://stackoverflow.com/a/46480745/9490861 !

Prajil Shrestha
  • 124
  • 1
  • 9
4

You have to import this in your module where you are using it:

import {MatExpansionModule} from '@angular/material/expansion';
Mustafa Kunwa
  • 821
  • 1
  • 9
  • 19
0

whatever you use like mat-form-field, mat-expansion-panel etc you need to import that in your .ts file of that component as well as at the app.module.ts file.

After you

import {MatExpansionModule} from '@angular/material/expansion';

and

@NgModule({
imports:[
MatExpansionModule,]
})

the you need to import again

import {MatExpansionModule} from '@angular/material/expansion'; 

this in your particular .ts file of the component.