question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide information on loaded lazy routes on RouteConfigLoadEnd

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior When accessing modules lazily, RouteConfigLoadEnd gets fired once the factory gets loaded, but before routes are loaded and assigned to route._loadedConfig.

Expected behavior RouteConfigLoadEnd should be fired after _loadedConfig is assigned, or return routes as a parameter if it’s fired from RouterConfigLoader.

Change proposal:

return map.call(moduleFactory$, (factory: NgModuleFactory<any>) => {
  const module = factory.create(parentInjector);
  const routes = flatten(module.injector.get(ROUTES));

  if (this.onLoadEndListener) {
    this.onLoadEndListener(route, routes);
  }

  return new LoadedRouterConfig(routes, module);
});

What is the motivation / use case for changing the behavior? Most common use case of RouteConfigLoadEnd is to react once lazy loaded routes are loaded. In localize-router we can use this information for routes localization. However if even is fired before routes are de facto available in _loadedConfig and/or it doesn’t provide this information it makes the entire event unusable as it’s not that different from RouteConfigLoadStart.

Issue is already mentioned here: https://github.com/angular/angular/issues/14036

  • Angular version: 4.0.X
  • Browser: all

  • Language: all

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
meeroslavcommented, Nov 29, 2017

@marko999, actually localize-router no longer depends on this.

With the help from @vsavkin, there is a workaround using SystemJsNgModuleLoader that solves the lazy loading problem and does not require changes in the Router’s code.

So unless there are some other interested parties in this feature, this issue can be closed.

@conor-mac-aoidh, @brandonroberts, @johnpapa, are you still interested in having this event as mentioned in #14036?

Maybe extending SystemJsNgModuleLoader would work for you as well?:

export class YourCustomLoader extends SystemJsNgModuleLoader {

  load(path: string): Promise<NgModuleFactory<any>> {
    return super.load(path).then((factory: NgModuleFactory<any>) => {
      return {
        moduleType: factory.moduleType,
        create: (parentInjector: Injector) => {
          const module = factory.create(parentInjector);
          const getMethod = module.injector.get.bind(module.injector);

          module.injector['get'] = (token: any) => {
            const getResult = getMethod(token);

            if (token === ROUTES) {
              // This is the place where routes are loaded, but not yet returned
              // Do some custom magic here, throw a custom event etc.
              return getResult;
            } else {
              return getResult;
            }
          };
          return module;
        }
      };
    });
  }
}

You can see how it was (mis)used in localize-router here.

2reactions
TaridaGeorgecommented, Aug 30, 2017

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 - Router instance for lazy loaded components
Lazy loaded routes have different events. See here. Specifically, see RouteConfigLoadStart and RouteConfigLoadEnd .
Read more >
Showing A Loading Indicator For Lazy-Loaded Route ...
Ben Nadel demonstrates how to use the Router's RouteConfigLoadStart and RouteConfigLoadEnd navigation events in order to show a " loading ...
Read more >
Adding routes dynamically to lazy loaded modules in Angular
Lazy loading of a module can be detected by subscribing to the RouteConfigLoadEnd router event. This event will be triggered after a module ......
Read more >
Showing A Loading Indicator For Lazy-Loaded Route ... - Vimeo
Ben Nadel demonstrates how to use the Router's RouteConfigLoadStart and RouteConfigLoadEnd navigation events in order to show a " loading ...
Read more >
RouteConfigLoadEnd - Angular
An event triggered when a route has been lazy loaded. class RouteConfigLoadEnd { constructor(route: Route) type: EventType.RouteConfigLoadEnd route: Route ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found