Angular2 RC and dynamic routes

痞子三分冷 提交于 2019-12-13 13:33:21

问题


We have a SPA app written in Angular which takes advantage of loading routes dynamically (as opposed to statically defined routes).

Over time, the number of these dynamically loaded UI components will grow and grow. For any given customer deployment, we will only be using a small subset of the possible components. At runtime, the list of components will be data driven. So my desire is to take that list of components and do the following: - define an application route for each component at runtime - lazy load that component

It’s a problem that has been solved in the Angular 1 version of our application (using ocLazyLoad package and angular’s $stateProvider)

It appears to be solvable in some of the later Beta releases of Angular2 (using AsyncRoutes and the router.config method - see technique here)

But in RC1 that AsyncRoutes and router.config method seems to be broken.

I can find very little with respect to guidance for loading components/routes asynchronously in the Angular2 release candidates.

Is there a canonical example for doing this with the latest candidates?


回答1:


In the new router (>= RC.3) https://angular.io/docs/ts/latest/api/router/index/Router-interface.html#!#resetConfig-anchor resetConfig can be used

router.resetConfig([
 { path: 'team/:id', component: TeamCmp, children: [
   { path: 'simple', component: SimpleCmp },
   { path: 'user/:name', component: UserCmp }
 ] }
]);

Lazy loading components is now (RC.5) implemented using lazy modules.

https://github.com/angular/angular/issues/11437#issuecomment-245995186 provides an RC.6 Plunker




回答2:


With RC5 ng2 team finally found a way to dynamically load router 1. AppModule is introduced and every is modulised ( back to AngularJS 1 date!) 2. "loadChildren" let you can load from files from disk:

{path: screenId, loadChildren: 'app/screens/' + screenId + '.module' })

Here is my sample code: http://plnkr.co/edit/nm5m7N?p=preview



来源:https://stackoverflow.com/questions/37623306/angular2-rc-and-dynamic-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!