angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Add loaded routes from REST in runtime

Open bjorkblom opened this issue 9 years ago • 38 comments

There's a bug when adding routes dynamically using router.resetConfig() method. I'm not sure if it's a bug in Angular-CLI, Angular compiler, Angular Router or Webpack, please guide me.

This is the setup:

## routes.json
[
    { "path": "contact", "loadChildren": "./contact/contact.module#ContactModule" }
]

## RouterService.ts
getRotues() {
   return this._http.get('routes.json')
     .map((res) => res.json())
}

Now to the actual bug:

let routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full'},
  { path: 'home', loadChildren: './home/home.module#HomeModule' }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ]
})

export class AppModule {
  constructor(private router:Router, private routerService:RouterService) {
    // This works
    // Pushing the same route as in routes.json
    // ----------------------
    routes.push({ path: 'contact', loadChildren: './contact/contact.module#ContactModule' })
    router.resetConfig(router);

    // This does not work
    // Loads json data from service and push the new route
    // ----------------------
    this.routerService.getRoutes().subscribe((result) => {
      result.forEach((route) => {
        routes.push({ path: route.path, loadChildren: route.loadChildren })
      });
      this.router.resetConfig(routes);
    });

    // This does works
    // By just console log this anonymous object, the loaded routing from the 
    // json data will work. 
    // ----------------------
    console.log({ loadChildren: './contact/contact.module#ContactModule' })
    this.routerService.getRoutes().subscribe((result) => {
      result.forEach((route) => {
        routes.push({ path: route.path, loadChildren: route.loadChildren })
      });
      this.router.resetConfig(routes);
    });
  }

Please provide us with the following information:

OS?

Windows 7

Versions.

Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here: angular-cli: 1.0.0-beta.21 (Also tried with Angular-cli 1.0.0-beta.26 with same result) node: 5.8.0 os: win32 x64

Repro steps.

The app was created by Angular-CLI, setup already described.

The log given by the failure.

Error: Uncaught (in promise): Error: Cannot find module './contact/contact.module'. Error: Cannot find module './contact/contact.module'. at webpackAsyncContext (main.bundle.js:64815) at SystemJsNgModuleLoader.loadAndCompile (main.bundle.js:73204) at SystemJsNgModuleLoader.load (main.bundle.js:73196) at RouterConfigLoader.loadModuleFactory (main.bundle.js:20568) at RouterConfigLoader.load (main.bundle.js:20559) at MergeMapSubscriber.project (main.bundle.js:76506) at MergeMapSubscriber._tryNext (main.bundle.js:16175) at MergeMapSubscriber._next (main.bundle.js:16165) at MergeMapSubscriber.Subscriber.next (main.bundle.js:440) at ScalarObservable._subscribe (main.bundle.js:41699) at webpackAsyncContext (main.bundle.js:64815) at SystemJsNgModuleLoader.loadAndCompile (main.bundle.js:73204) at SystemJsNgModuleLoader.load (main.bundle.js:73196) at RouterConfigLoader.loadModuleFactory (main.bundle.js:20568) at RouterConfigLoader.load (main.bundle.js:20559) at MergeMapSubscriber.project (main.bundle.js:76506) at MergeMapSubscriber._tryNext (main.bundle.js:16175) at MergeMapSubscriber._next (main.bundle.js:16165) at MergeMapSubscriber.Subscriber.next (main.bundle.js:440) at ScalarObservable._subscribe (main.bundle.js:41699) at resolvePromise (main.bundle.js:101365) at resolvePromise (main.bundle.js:101350) at main.bundle.js:101399 at ZoneDelegate.invokeTask (main.bundle.js:101162) at Object.onInvokeTask (main.bundle.js:36074) at ZoneDelegate.invokeTask (main.bundle.js:101161) at Zone.runTask (main.bundle.js:101051) at drainMicroTaskQueue (main.bundle.js:101298) at HTMLElement.ZoneTask.invoke (main.bundle.js:101236)

Mention any other details that might be useful.


Thanks! We'll be in touch soon.

bjorkblom avatar Jan 25 '17 22:01 bjorkblom

I created a component that dynamically loads other components. I have data loaded from a CMS and the component type is included in that data, when it loads I destroy the old component and create a new one and pass in the data. I have a single wildcard route that always loads of dynamic component and I use the route to tell the server what data I need. It works pretty well and means I don’t have to specify and of the routes in angular at all, they are all managed in the CMS.

Beavykins avatar Oct 11 '17 06:10 Beavykins

@Beavykins are you loading the components Lazily? Can you explain how did you exactly achieve that?

@filipesilva : @Meligy Can we compile the lazy loaded modules using AOT and read them from the JSON file?

NaveenTula90 avatar Oct 11 '17 15:10 NaveenTula90

@NaveenTula90 no the components are compiled as part of the app, the dynamic component has references to these components so I can load them in when needed. It’s only the data I am lazy loading and that contains the same reference to the required component. For me it’s way too limiting to have all the routes hard coded as the CMS can let them change routes and use any component anywhere.

Beavykins avatar Oct 11 '17 17:10 Beavykins

@Beavykins The thing which @NaveenTula90 saying is for a large enterprise app, where the developer would give the routes and path to the component in a JSON file rather than messing with the modules. If you are talking about having reference to the component, I don't think so we can have that many references...

ramasamykasiviswanathan avatar Oct 12 '17 09:10 ramasamykasiviswanathan

@ramasamykasiviswanathan I guess the principle is the same though. I am getting the data for the route and a reference to the component that is going to be used, it’s just a key in the object that contains the reference to the real component. Instead of that it it could just be JSON with the routes and the component to use and refer to that when the route changes. But if you are also loading data from somewhere else to populate the component why not just pass in the component with the route data. I guess the downside is that you have to wait for the data before you can show the right component that way, but I have an animation on route change so you don’t see the delay.

Beavykins avatar Oct 12 '17 12:10 Beavykins

@Beavykins Can you share a sample code so that we can get better idea on what you were saying? just a snippet would be fine? because it is going to be the base for enterprise level large application.

ramasamykasiviswanathan avatar Oct 12 '17 16:10 ramasamykasiviswanathan

@mecp could you share a sample code? I think your solution would be the best but I can't get it to work. below is my code. thanks very much!

import { Shemesh4Component } from './shemesh4/shemesh4.component';
import { RouterModule, Routes, Router } from '@angular/router';
let routes: Routes = [
  {
    path: 'dummy-path',
    loadChildren: '../shemesh5/shemesh5.module#Shemesh5Module'
  },
  {
    path: '',
    loadChildren: '../shemesh6/shemesh6.module#Shemesh6Module'
  }

];


@NgModule({
  imports: [
    CommonModule, RouterModule.forChild(routes)
  ],
  declarations: [Shemesh4Component]
})
export class Shemesh4Module {
  constructor(private router: Router) {
    // This works
    // Pushing the same route as in routes.json
    // ----------------------
    routes[0].path = 'realpath-fromconfig';
    router.resetConfig(routes);
  }
}

lmessinger avatar Oct 16 '17 16:10 lmessinger

I am also try to do the same but no luck. map is not generated while push routes from json

bechar-ce avatar Oct 24 '17 10:10 bechar-ce

If id do below code and publish the build it's work fine but same file read from JSON map not generated...

const routes = [ { path: 'about', loadChildren: 'app/components/about/about.module#AboutModule' } { path: 'about', loadChildren: 'app/components/about/about.module#AboutModule' }, { path: 'home', loadChildren: 'app/components/home/home.module#HomeModule' }, { path: 'external', loadChildren: 'app/components/external/external.module#ExternalModule' } ];

bechar-ce avatar Oct 24 '17 10:10 bechar-ce

ok, take a look here: https://github.com/sharpangles/platform/tree/master/modules/angular-dynamic

this is exactly what everyone here needs. thanks @sharpangles !

lmessinger avatar Oct 25 '17 10:10 lmessinger

@lmessinger Could you help me out how we can bootstrap the repo shared by you.

Appreciate your quick help.

bechar-ce avatar Oct 26 '17 10:10 bechar-ce

@lmessinger

Also any working demo for the above scenario would be fine where in routes are getting activated dynamically!

rahulsahay19 avatar Nov 08 '17 08:11 rahulsahay19

the documentation explains it. In essence the loading is throught an html template that lists the components to be lazy loaded

On Wed, Nov 8, 2017 10:06 AM, rahul sahay [email protected] wrote: @lmessinger

Also any working demo for the above scenario would be fine where in routes are getting activated dynamically!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Lior Messinger +1-646-3730044+972-546-888401

lmessinger avatar Nov 08 '17 08:11 lmessinger

you need to invoke AOT compiler at runtime, somehow... like the webpack loader and plugin does at build time

gizm0bill avatar Dec 14 '17 10:12 gizm0bill

@mecp Can you provide your solution, how you achieved the same. It will be great help.

Thanks, Rahul

rahulsahay19 avatar Apr 02 '18 09:04 rahulsahay19

I have a possible solution in my mind and want to share it with you. Our scenario is that our lazy routes come from other NPM packages within the node_modules folder.

  • Put all your packages which provide new lazy routes into one NPM namespace via the @annotation e.g. @company-name/lazy-modules/module-name
  • Put the lazy route information into the modules package.json as meta info (see https://stackoverflow.com/a/27232456/2664516)
  • Write a small Webpack plugin which goes through your NPM namespace, collect all the routing information and write a app.routes.ts file with all the static routes in it

With that you end up with proper static routes and decoupled modules with the benefit of being flexible by adding/removing NPM packages as you wish without the need of touching your code by hand.

ThYpHo0n avatar Apr 05 '18 09:04 ThYpHo0n

I have the same requirement. Will it work if I put in my app routes just a single route to a lazyloaded module. This module will just be a slim dummy module (with an obscure name and always guarded) that has also routes that I basically use to populate and build all my other modules that I want lazyloaded? Then at runtime I can pull down my new app routes and build my actual routes dynamically. This way all my modules are built. Im wondering if something like this would work.

michaelrgalloway avatar Apr 17 '18 01:04 michaelrgalloway

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.