angularlazy-loadingangular-resolver

resolver on lazy loading angular


is there a way to add a resolver before loading a lazy load module? i tried to add resolve to the routs configuration but it is not triggered, also didn't find any thing useful about it on the web. any help would be appreciated

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
// services
import {SecondResolverService} from "./second.resolver.service";

const routes: Routes = [
  { path: 'first' , loadChildren: './first/first.module#FirstModule' },
  { path: 'second', loadChildren: './second/second.module#SecondModule' ,resolve : {data : SecondResolverService}}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Solution

  • Angular docs on lazy loading

    This config always works for me. If this doesnt work there may be something wrong with your resolver.

    const routes: Routes = [
      {
        path: 'second',
        loadChildren: () => import('/second/second.module')
          .then(m => m.SecondModule),
        resolve: {
          data: SecondResolverService
        }
      },