10

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 { }
omer
  • 2,435
  • 2
  • 24
  • 28
  • What do you main about "before loading a lazy load module"? I have implemented routing like that and it works, when the navigation finish I got the data requested. – Octavio Garbarino Nov 17 '18 at 00:47
  • do you use a resolve in your routing? – omer Nov 17 '18 at 00:48
  • Yes, I do. The only difference is that I do it in a child module not in app module. So I have appmodule -> ChildOneModule -> ChildTwoModule, and in the routing of the ChildOneModule I have a resolver when resolving a path to ChildTwoModule and I have the data in all routes of ChildTwoModule... The only difference is that i don't export the RouterModule, I just import it like you do (in child I use forChild insted of forRoot but is the same) and I import in my AppModule the AppRoutingModule – Octavio Garbarino Nov 17 '18 at 01:01
  • nope, don't think so. you can add a resolver to the eager route immediately *before* the lazy loaded route – Michael Kang Nov 17 '18 at 01:38
  • @pixelbits - it doesn't make sense if it is resolver is required for `Feature Module` which is lazy module then why resolver should be declared in `Eager Module`. – Sunil Singh Nov 17 '18 at 04:54
  • That’s just the way it is – Michael Kang Nov 17 '18 at 04:55
  • @omer did you figure this out ? – glemiere Jan 10 '19 at 03:46
  • Yes I did will upload my solution in a few hours – omer Jan 10 '19 at 10:53
  • I didn’t figure it out I have half a solution that’s way I didn’t post yet.. – omer May 15 '19 at 15:54

2 Answers2

7

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
    }
  },
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
ejnickles
  • 86
  • 1
  • 3
1

I have encountered the same issue. Here is an example of this issue in Stackblitz.com in Angular 9

https://stackblitz.com/edit/angular-ivy-ubes1q?file=src%2Fapp%2Fapp.module.ts

The ChildResolver is set on the lazy-loaded route, but never gets called.


My workaround was to make a "lazyResolver" inside my guard.

So something like this in data.guard.ts:

export class MyGuard implements CanActivate, CanActivateChild {

  constructor(
    private router: Router,
    private authService: AuthService,
    private myService: MyService,
  ) { }

  public canActivate(route, state): Observable<boolean | UrlTree> {
    return this.lazyResolver$().pipe(
      map(data => {
        // now we lazy resolved!
        return true;
      }),
      take(1),
      catchError(error => EMPTY),
    );
  }

  // safeguard when using resolvers and guards with lazy-loaded modules
  private lazyResolver$(): Observable<any> {
    return new MyResolver(this.myService).resolve(route, state),
  );

}

Federico
  • 49
  • 2
  • Link-only answers are discouraged on Stack Overflow, as they are rendered useless if the link stops working or the content significantly changes. You should [edit] your answer to include the most important details that are relevant to the question in the answer itself. Answers that are little more than a link somewhere else may end up being [deleted](https://stackoverflow.com/help/deleted-answers). – Hoppeduppeanut Jun 15 '20 at 23:50
  • 1
    Updated my comment with example workaround. Thanks. – Federico Jun 16 '20 at 18:13
  • I created an issue in github for this, maybe others in the Angular world can shine a light on this issue or non-issue. https://github.com/angular/angular/issues/37625 – Federico Jun 17 '20 at 14:44