ocombe / ocLazyLoad

Lazy load modules & components in AngularJS
https://oclazyload.readme.io
MIT License
2.63k stars 510 forks source link

AngularJS: Broken lazy loaded modules on state removal #422

Open realph opened 6 years ago

realph commented 6 years ago

I'm using ocLazyLoad in combination with UI-Router to lazy load my modules on state transition.

const state = {
  name: 'home.**',
  url: '/home',
  lazyLoad = transition => {
    transition
      .injector()
      .get('$ocLazyLoad')
      .inject('home-module.ts');
  };
}

$stateRegistry.register(state);

This works great. I'm able to visit that state which lazy loads home-module.ts, which in turn registers the components that are a part of home-module.ts. This all works as expected.

I then want the ability to de-register that very state, and reregister the same state.

$stateRegistry.deregister(state);
$stateRegistry.register(state);

If I now revisit the home.** state the module is not lazy loaded and its components aren't registered. I'm trying to understand what's going on here.

I have a feeling that when we deregister the state, something happens to that lazy loaded module - it's not unloaded because we cannot do that in JS. I'm guessing that the namespace (the module name) creates a pointer that references the original state name (home.**) and when we deregister the state, that pointer is broken. I'm out of ideas at this point.

Any help on this would be appreciated. Thanks in advance!

vort3xxx commented 6 years ago

If you don't mind me asking, what's the use case for deregistering a state at runtime? That's something I've never actually seen done in the wild and I'd like to know more.