Lazy Loading Modules Based on Path Param

598 Views Asked by At

I am trying to conditionally load modules (built for varying layouts) based on data received from the API. The data is fetched based on a value of a pathparam. I have tried following route config below:

path: ':objectId',
loadChildren: async () => {
      const service = AppInjector.get(ObjectService);
      const activatedRoute = AppInjector.get(ActivatedRouteSnapshot);
      const obj = await ObjectService.get(activateRoute.paramsMap['objectId']);
      if (obj.type === 'one') {
        const a = await import('./modules/layouts/one.module');
        return a.OneModule;
      }
      const b = await import('./modules/layouts/two.module');
      return b.TwoModule;
    }
  }

This throws a Null Injector error for ActivatedRouteSnapshot.

I have also tried to use ActivatedRoute with subscribe but the pathparam is not filled in. It seems as though the parameter is filled in after loadChildren has been executed.

Any help on how can achieve above would be great.

Thanks!

0

There are 0 best solutions below