Here is a simplified version of the main structure you need, consisting of 2 levels (main level with forRoot, and other modules level loaded using "Lazy loading" with forChild). Feel free to make it more complex by adding guards, nested levels, etc.
Below is an example structure (make sure to customize it with your own components):
- MainModule (app-module.ts)
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './components/App.component';
import { PageAComponent} from './modules/module-a/components/page-a.component';
import { PageBComponent} from './modules/module-b/components/page-b.component';
const routes: Routes = [
{
path: '',
component: MainComponent ,
},
{
path: 'patha',
component: PageAComponent,
children: [{
path: '',
loadChildren: () => import('./modules/module-a/module-a.module').then(m => m.AModule)
}],
},
{
path: 'pathb',
component: PageBComponent,
children: [{
path: '',
loadChildren: () => import('.modules/module-b/module-b.module').then(m => m.BModule)
}],
{
path: '**',
redirectTo: '',
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
- AModule
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PageAComponent } from './pages/page-a/page-a.component';
import { RegisterComponent } from './components/register/register.component';
import { LoginComponent } from './components/login/login.component';
const routes: Routes = [
{
path: '', // takes the "base path" defined in its parent routes (in app.module.ts)
component: PageAComponent,
pathMatch: 'full',
children: [
{
path: 'login', // For instance
component: LoginComponent,
},
{
path: 'register', // For instance
component: RegisterComponent,
},
{
path: '**',
redirectTo: 'login',
},
],
},
];
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [
RouterModule
]
})
export class AModule { }
- BModule
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PageBComponent } from './pages/page-b/page-b.component';
import { ListComponent} from './components/list/register.component';
import { EditComponent} from './components/edit/edit.component';
const routes: Routes = [
{
path: '', // takes the "base path" defined in its parent routes (in app.module.ts)
component: PageBComponent,
pathMatch: 'full',
children: [
{
path: 'list', // For instance
component: ListComponent,
},
{
path: 'edit', // For instance
component: EditComponent,
},
{
path: '**',
redirectTo: 'list',
},
],
},
];
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [
RouterModule
]
})
export class BModule { }
In the AppComponent, you would have your main <router-outlet></router-outlet>
Then, in PageAComponent and PageBComponent, you would only have <router-outlet></router-outlet> as well. These components act as containers for child module components.
These are the basic structures you can use, but feel free to modify the names of components, paths, and modules to fit your specific needs.