Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 { }
like image 714
omer Avatar asked Nov 17 '18 00:11

omer


People also ask

What does resolver do in Angular?

So what is Angular Resolver? Angular Route Resolver is used for pre-fetching some of the data when the user is navigating from one route to another. It can be defined as a smooth approach for enhancing user experience by loading data before the user navigates to a particular component.

What does loadChildren do in Angular?

LoadChildrenlink A function that returns a set of routes to load.

Is lazy loading good in Angular?

Lazy loading helps to keep the bundle size small, which helps reduce load times. We must use the class decorator to create an Angular module @NgModule, and the decorator uses a metadata object that defines the module. The main properties are: import: Components of this module are used with Array with other modules.


1 Answers

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
    }
  },
like image 107
ejnickles Avatar answered Oct 08 '22 03:10

ejnickles