Angular material not working in angular version 9

11,112

Solution 1

You may need to also add any material modules to the exports like so:

import { NgModule } from '@angular/core'; 

import {MatButtonModule} from '@angular/material/button';

@NgModule({
   declarations: [],
   imports: [ MatButtonModule ],
   exports: [ MatButtonModule ]
})

export class MaterialModule { }

This is an example from a material.module I am using in a project:

import { NgModule } from '@angular/core';

import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { MatToolbarModule } from '@angular/material/toolbar';

const mat = [
  MatButtonModule,
  MatIconModule,
  MatMenuModule,
  MatSidenavModule,
  MatSlideToggleModule,
  MatToolbarModule,
];

@NgModule({
  imports: [
    ...mat,
  ],
  exports: [
    ...mat,
  ],
})
export class MaterialModule { }

This way everything that is added to the array will be imported and exported to other modules.

Solution 2

For those of you who are still struggling, and I don't know why this issue has been down voted, I just restarted my visual studio code and it worked for me.

Solution 3

Also, remember to add this in your global (styles.css) styled @import "~@angular/material/prebuilt-themes/indigo-pink.css";

This was the issue for me after a series of upgrades, coming from version 9 up to 12.

Solution 4

The angular material library (@angular/material/*) has not been processed correctly by ngcc, or is not compatible with Angular Ivy(vs.9).

Add in package.json

"scripts": {
    "postinstall": "ngcc"
  },

Share:
11,112
Shubham Patil
Author by

Shubham Patil

Updated on June 15, 2022

Comments

  • Shubham Patil
    Shubham Patil almost 2 years

    I'm updated the angular version 8 to angular 9 and also updated angular material to version 9 but it's not working and not showing any error. how to fix it.

    here is code material module

    import { NgModule } from '@angular/core';
    import {MatButtonModule} from '@angular/material/button';
    
     @NgModule({
      declarations: [],
      imports: [
        MatButtonModule
      ] 
    })
    export class MaterialModule { }
    

    app-component.html

    <h3>Stroked Buttons</h3>
     <div class="example-button-row">
      <button mat-stroked-button>Basic</button>
      <button mat-stroked-button color="primary">Primary</button>
      <button mat-stroked-button color="accent">Accent</button>
      <button mat-stroked-button color="warn">Warn</button>
      <button mat-stroked-button disabled>Disabled</button>
      <a mat-stroked-button routerLink=".">Link</a>
     </div>
    

    app.module

     import { BrowserModule } from '@angular/platform-browser';
     import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
     import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { BrowserAnimationsModule } from '@angular/platform browser/animations';
     import { MaterialModule } from './angular-material/material/material.module';
      import { MatButtonModule } from '@angular/material/button';
    
    
    
    @NgModule({
     declarations: [
      AppComponent
     ],
     imports: [
       BrowserModule,
       AppRoutingModule,
       BrowserAnimationsModule,
        MaterialModule,    
      ],
      providers: [],
      bootstrap: [AppComponent],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
     })
     export class AppModule { }
    
  • Sangram Badi
    Sangram Badi about 4 years
    is it required to create MaterialModule ts file
  • Nicholas Pesa
    Nicholas Pesa about 4 years
    No not required. You could import all of the modules in the app.module.ts or any other module declared. This is just for maintainability.
  • MJ X
    MJ X almost 4 years
    There is some issues with Angular 9 and Angular Material, I did add in export and import but I get error: This likely means that the library (@angular/material/sidenav) which declares MatSidenav has not been processed correctly by ngcc, or is not compatible with Angular Ivy
  • Naveen Kumar V
    Naveen Kumar V almost 4 years
    @MJX Did you find any solution for it?