Import into lazy loaded module in Angular5

Issue

Trying to group some components into lazy loading modules and getting an issue with material imports:

error: Error: StaticInjectorError(AppModule)[MatCheckbox -> InjectionToken mat-checkbox-click-action]: 
  StaticInjectorError(Platform: core)[MatCheckbox -> InjectionToken mat-checkbox-click-action]: 
    NullInjectorError: No provider for InjectionToken mat-checkbox-click-action!)

Same error appears for most of material components (e.g. mat-checkbox), but for example it works fine with mat-icon:

#working fine:
<mat-icon>phone</mat-icon>
<mat-slider min="1" max="5" step="0.5" value="1.5"></mat-slider>

#errors:
<mat-slide-toggle>Slide me!</mat-slide-toggle>
<mat-radio-group>
   <mat-radio-button value="2">Option 2</mat-radio-button>
</mat-radio-group>
<mat-checkbox>Hello world</mat-checkbox>

I have following architecture of the app:

  • app.module imports dashboard.module in non-lazy way
  • dashboard.module imports test.module in lazy way
  • test.module has a component with error described above

What type of problem is this? Am I missing some import?
Is it OK to have lazy loading in sub-modules as in this case?

EDIT:

#app.module.ts (app module simplified)

import { DashboardModule } from './dashboard/module';
import { AppComponent } from './app.component';
import { RouterModule, Routes, PreloadAllModules } from '@angular/router';
import {MatCheckboxModule} from '@angular/material/checkbox';


const routes: Routes = [
    {
        path: 'dashboard',
        redirectTo: 'dashboard/test',
        pathMatch: 'full',
    }
];

@NgModule({
    declarations: [
      AppComponent
    ],
    imports: [
        RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules }),
        DashboardModule,
        MatCheckboxModule,

   ],
   bootstrap: [AppComponent],
export class AppModule {
}

#module.ts (dashboard module simplified)

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DashboardResolver } from './test/resolver';
import { DashboardComponent } from './test/component';

const dashboardRoutes: Routes = [
    {
        path: 'dashboard',
        component: DashboardComponent,
        children: [
            {
                path: 'test',
                pathMatch: 'full',
                loadChildren: './test/module#DashboardTestModule',
                data: {loginStatusShouldBe: true},
                resolve: {
                    test: DashboardResolver,
            },
        },
        ]
    }
];

@NgModule({
    imports: [
        CommonModule,
        RouterModule.forChild(dashboardRoutes),
    ],
    exports: [
        RouterModule,
    ],
    providers: [
        DashboardResolver,
    ],
export class DashboardModule {
}

#module.ts (test module simplified)

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TestComponent } from './component';
import { RouterModule, Routes } from '@angular/router';

const testRoutes: Routes = [
    {
        path: '',
        component: TestComponent,
    },
];

@NgModule({
    imports: [
        CommonModule,
        RouterModule.forChild(testRoutes),
    ],
    declarations: [
        TestComponent,
    ],
    exports: [
        RouterModule,
    ],
})
export class DashboardTestModule {
}

Solution

Seems like you are missing provider in your module. Add MAT_CHECKBOX_CLICK_ACTION in to your app module:

providers: [
    ...,
    MAT_CHECKBOX_CLICK_ACTION
]

or MatCheckboxModule in to your app module:

imports: [
    ...,
    MatCheckboxModule
]

Answered By – Dipen Shah

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.