I am using the library nest-router and I want to split router structure by many files attached to controllers or just file stored near controller.
In Angular i can use loadChildren in Router definition and make lazy loading module.
How i can create router structure like Angular style? Or just separate router file by child routes?
const routes: Routes = [
{
path: '/ninja',
module: NinjaModule,
childrens: NinjaRouterModule // <- like here
},
];
in NinjaRouterModule
const routes: Routes = [
{
path: '/cats',
module: CatsModule,
},
{
path: '/dogs',
module: DogsModule,
childrens: DogsRouterModule // <-like here
}
];
NestJS 8x
import { Module } from '@nestjs/common'
import { RouterModule } from '@nestjs/core'
@Module({
imports: [RouterModule.register([{
path: 'admin',
module: AdminModule,
children: [
{
path: 'dashboard',
module: DashboardpModule
},
{
path: 'metrics',
module: MetricsModule
}
]
}])]
})
export default class AppModule {
}
https://docs.nestjs.com/recipes/router-module#router-module
You need to import all your children routes in main module(AppModule) along with RouterModule.register
@Module({
imports: [
NinjaRouterModule,
CatsModule,
DogsModule,
DogsRouterModule,
RouterModule.register(routes),
],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With