New to Angular and trying to structure my code in modules. I got an admin module responding to
/admin
request and now I'm trying to add another child module to the Admin module, called Portfolio Module.
This works, except that I want the portfolio module, to response to
/admin/portfolio
request. Right now it response to
/portfolio
request instead.
Here I'm Importing the PortfolioModule
admin.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminRoutingModule } from './admin-routing.module';
import { IndexComponent } from './shared/index/index.component';
import {PortfolioModule} from './portfolio/portfolio.module';
@NgModule({
declarations: [IndexComponent],
imports: [
CommonModule,
PortfolioModule,
AdminRoutingModule
]
})
export class AdminModule { }
Thinking that I maybe need to define the PortfolioModule as a children route. inside the admin route.
admin-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { IndexComponent } from './shared/index/index.component';
const routes: Routes = [
{
path: 'admin',
component: IndexComponent,
children: [
{
path: 'portfolio',
/*Maybe add Portfolio Module here?*/
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AdminRoutingModule { }
Here is my portfolio module, nothing special about it.
portfolio.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PortfolioRoutingModule } from './portfolio-routing.module';
import { IndexComponent } from './index/index.component';
import { CreateComponent } from './create/create.component';
import { ListComponent } from './list/list.component';
import { UpdateComponent } from './update/update.component';
@NgModule({
declarations: [IndexComponent, CreateComponent, ListComponent, UpdateComponent],
imports: [
CommonModule,
PortfolioRoutingModule
]
})
export class PortfolioModule { }
And last here is my portfolio routing, maybe I'm missing something here, to tell it to include the parent route.
portfolio-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { IndexComponent } from './index/index.component';
import { ListComponent } from './list/list.component';
import { CreateComponent } from './create/create.component';
import { UpdateComponent } from './update/update.component';
const routes: Routes = [
{
path: 'portfolio',
component: IndexComponent,
children: [
{
path: 'list',
component: ListComponent
},
{
path: 'create',
component: CreateComponent
},
{
path: 'update',
component: UpdateComponent
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class PortfolioRoutingModule { }
When you lazy load, a module the root of the path should be in the routing of the module that is declaring the lazy loaded module.
For instance, path: 'portfolio'
would be declared as a route in the admin-routing.module
and your IndexComponent
for portfolio-routing.module
would have an empty path (path: ''
). Also the children
attribute is probably not necessary as all routes would be on the same level.
Your routing could look something like this.
...
const routes: Routes = [
{
path: 'admin',
loadChildren: './admin/admin.module#AdminModule'
}
]
...
...
const routes: Routes = [
{
path: '',
component: AdminIndexComponent
},
{
path: 'portfolio',
loadChildren: './portfolio/portfolio.module#PortfolioModule'
}
]
...
...
const routes: Routes = [
{
path: '',
component: PortfolioIndexComponent
},
{
path: 'create',
component: PortfolioCreateComponent
}
]
...
I also put together a working stackblitz of this example linked below along with the angular guide doc for lazy loading feature modules.