Search code examples
angularangular-materialangular-material2angular9

Angular material not working in angular version 9


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

Solution

  • 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.