I am in the process upgrading an application I\'m working on to the latest Angular 2 release candidate. As part of this work I am attempting to use the NgModule spec and mig
Okay, after fiddling around with this for the better part of the weekend I got it running on my end. What worked for me in the end was to do the following:
Routes for every module you want to route. Do not import any of the RouterModule.forChild() in the child modules.import keyword) all child routes as usual and use the ... operator to incorporate these under the correct path. I couldn't get it to work with the child-module defining the path, but having it on the parent works fine (and is compatible to lazy loading).In my case I had three levels in a hierarchy like this:
/)
editor/:projectId)
query/:queryId)page/:pageId)about)The following definitions work for me for the /editor/:projectId/query/:queryId path:
// app.routes.ts
import {editorRoutes} from './editor/editor.routes'
// Relevant excerpt how to load those routes, notice that the "editor/:projectId"
// part is defined on the parent
{
path: '',
children: [
{
path: 'editor/:projectId',
children: [...editorRoutes]
//loadChildren: '/app/editor/editor.module'
},
]
}
The editor routes look like this:
// app/editor/editor.routes.ts
import {queryEditorRoutes} from './query/query-editor.routes'
import {pageEditorRoutes} from './page/page-editor.routes'
{
path: "", // Path is defined in parent
component : EditorComponent,
children : [
{
path: 'query',
children: [...queryEditorRoutes]
//loadChildren: '/app/editor/query/query-editor.module'
},
{
path: 'page',
children: [...pageEditorRoutes]
//loadChildren: '/app/editor/page/page-editor.module'
}
]
}
And the final part for the QueryEditor looks like this:
// app/editor/query/query-editor.routes.ts
{
path: "",
component : QueryEditorHostComponent,
children : [
{ path: 'create', component : QueryCreateComponent },
{ path: ':queryId', component : QueryEditorComponent }
]
}
However, to make this work, the general Editor needs to import and export the QueryEditor and the QueryEditor needs to export QueryCreateComponent and QueryEditorComponent as these are visible with the import. Failing to do this will get you errors along the lines of Component XYZ is defined in multiple modules.
Notice that lazy loading also works fine with this setup, in that case the child-routes shouldn't be imported of course.