Skip to content

Router event lazy loaded route #14036

Closed
Closed

Description

@conor-mac-aoidh

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

If a route is lazy loaded using a preloading strategy, there is currently no way of knowing when the route is loaded.

Expected behavior

It would be really useful if there was a router Event eg. RouteLazyLoaded that would trigger when a lazy loaded route is preloaded.

Minimal reproduction of the problem with instructions

this.router.events
  .filter(event => event instanceof RouteLazyLoaded)
  .subscribe(event => {
     // perform some action when route is loaded
  });

What is the motivation / use case for changing the behavior?

The use case is that the angular app can perform some action when the route is loaded.

In my case, I am constructing a multi-level navigation menu that will preload routes as each level is "opened". When the routes are preloaded, the navigation must be updated in order to include the new routes in the list.

Activity

  1. brandonroberts commented on Jan 20, 2017

    @brandonroberts
    Contributor
  2. conor-mac-aoidh commented on Jan 20, 2017

    @conor-mac-aoidh
    Author

    @brandonroberts Thanks for the link! Don't know how I missed that! FYI I did open a stack overflow question also, but got no responses. Thanks again and I'll close the issue.

  3. conor-mac-aoidh commented on Jan 23, 2017

    @conor-mac-aoidh
    Author

    @brandonroberts I've implemented a preloading strategy, however, I'm still unable to tell when a module is actually preloaded. This is my implementation:

    import 'rxjs/add/observable/of';
    import { Injectable } from '@angular/core';
    import {
      PreloadingStrategy,
      Route
    } from '@angular/router';
    import { Observable } from 'rxjs/Observable';
    
    @Injectable()
    export class SelectivePreloadingStrategy implements PreloadingStrategy {
      preloadedModules: Observable<any>[] = [];
    
      preload(route: Route, load: () => Observable<any>): Observable<any> {
        if (route.data && !route.data['preload']) {
          return Observable.of(null);
        }
        else {
          // log the route path to the console
          console.log('[SelectivePreloadingStrategy]: preloading module: ' + route.path);
    
          return load()
            .map(res => {
              // do something after preloaded
              console.log('module preloaded! res:  ', res);
              return res;
            });
        }
      }
    }

    The first console.log runs correctly for each module that I am preloading. However, the map callback never gets executed.. not sure why this is. Any ideas?

    Thanks again!

    Angular version: 2.4.4

  4. johnpapa commented on Jan 30, 2017

    @johnpapa
    Contributor

    I'd like to see this too, for lazy loaded and preloading events

  5. brandonroberts commented on Jan 30, 2017

    @brandonroberts
    Contributor

    @vicb will you reopen this one?

  6. DzmitryShylovich commented on Feb 1, 2017

    @DzmitryShylovich
    Contributor

    @conor-mac-aoidh Only route available at that point. So event signature would be something

    export class RouteLazyLoaded {
      constructor (public route: Route) {}
    }
    

    is it ok or do you need something else?

  7. conor-mac-aoidh commented on Feb 1, 2017

    @conor-mac-aoidh
    Author

    @DzmitryShylovich That would be fine! Thanks

  8. conor-mac-aoidh commented on Feb 3, 2017

    @conor-mac-aoidh
    Author

    @DzmitryShylovich any idea what version of Angular this feature might appear in?

  9. DzmitryShylovich commented on Feb 3, 2017

    @DzmitryShylovich
    Contributor

    @conor-mac-aoidh it's a feature so in 4.0

  10. conor-mac-aoidh commented on Feb 3, 2017

    @conor-mac-aoidh
    Author

    @DzmitryShylovich Ok great thanks

  11. conor-mac-aoidh commented on Feb 28, 2017

    @conor-mac-aoidh
    Author

    Thanks @DzmitryShylovich and @vicb the feature works great in ng4 rc.1 !

  12. conor-mac-aoidh commented on Feb 28, 2017

    @conor-mac-aoidh
    Author

    One thing @DzmitryShylovich , I would expect that by the time RouteConfigLoadEnd executes, the route._loadedConfig property would be populated. If not, what is the distinction between RouteConfigLoadStart and RouteConfigLoadEnd ?

  13. conor-mac-aoidh commented on Feb 28, 2017

    @conor-mac-aoidh
    Author

    Ok, makes sense. It does tell me when the route is loaded (HTTP request is complete).

    But it does not tell me when the route has been parsed by angular - ie. route data, route children are not accessible when the RouteConfigLoadEnd event fires.

    Could we pass routes in as a second argument eg:

    return map.call(moduleFactory$, (factory: NgModuleFactory<any>) => {
          const module = factory.create(parentInjector);
          const injectorFactory = (parent: Injector) => factory.create(parent).injector;
          const routes = flatten(module.injector.get(ROUTES));
    
          if (this.onLoadEndListener) {
            this.onLoadEndListener(route, routes);
          }
    
          return new LoadedRouterConfig(
              routes, module.injector, module.componentFactoryResolver,
              injectorFactory);
    });
  14. conor-mac-aoidh commented on Feb 28, 2017

    @conor-mac-aoidh
    Author

    Or, remove loadChildren and add children:

    return map.call(moduleFactory$, (factory: NgModuleFactory<any>) => {
          const module = factory.create(parentInjector);
          const injectorFactory = (parent: Injector) => factory.create(parent).injector;
          const routes = flatten(module.injector.get(ROUTES));
    
          if (this.onLoadEndListener) {
            delete route.loadChildren;
            route.children = routes;
            this.onLoadEndListener(route);
          }
    
          return new LoadedRouterConfig(
              routes, module.injector, module.componentFactoryResolver,
              injectorFactory);
    });
  15. DzmitryShylovich commented on Mar 3, 2017

    @DzmitryShylovich
    Contributor

    @conor-mac-aoidh please file a new issue

  16. added a commit that references this issue on Aug 11, 2017
    148718a
  17. added a commit that references this issue on Aug 28, 2017
    3d19f2b
  18. haskalach commented on Feb 22, 2019

    @haskalach

    @conor-mac-aoidh can you please post the final solution to be able to listen when each module is loaded ?!
    what i am trying to achieve is to make the custom pre-loading strategy to load each module one after another , since i have more than 30 modules to be loaded upon user login

  19. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  20. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions