Reputation: 33
I'm trying to create a nested route (I believe this is the term for it).
With one main route which supplies four entirely different pages (main-component.html is just the router outlet tags).
And inside the first route path, i want to have in one part of the page (center div) a second routing which will be navigated by arrow buttons (forward and backward). The content for this div is complex (including communication with a backend server) so a construction with *ngIf and different templates is not ideal).
Can this be done with two routes? And if so, how?
Upvotes: 3
Views: 5379
Reputation: 70
you can create lazy loading. top root module with routing. following step by step Gide.
index.html file
<body>
<app-root></app-root>
<body>
then create root.module.ts file and root-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { RootComponent } from './root.component';
import { from } from 'rxjs';
const routes: Routes = [
{
path: 'account',
loadChildren: './app/account/account.module#AccountModule',
data: { preload: true }
},
{
path: 'app',
loadChildren: './app/app.module#AppModule',
data: { preload: true }
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: []
})
export class RootRoutingModule { }
then create child account module and account routing. for following code
account-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AccountComponent } from './account.component';
import { LoginComponent } from '../login/login.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
const routes: Routes = [
{
path: '',
component: AccountComponent,
children: [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AccountRoutingModule { }
Upvotes: 0
Reputation: 38094
It can be done like this:
import { Routes, RouterModule } from '@angular/router';
import { FirstComponent,
SecondComponent,
ThirdComponent,
FourthComponent
} from './app.component';
const appRoutes: Routes = [
{
path: 'first',
component: FirstComponent,
children:[
{
path : 'second',
component: SecondComponent,
children:[
{
path : 'fourth',
component: FourthComponent
},
{
path : 'third',
component: ThirdComponent
}
]
}
]
},
{
path: '',
redirectTo: '/first',
pathMatch: 'full'
},
];
and components:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h3 class="title">Routing</h3>
<hr />
<router-outlet></router-outlet>
`
})
export class AppComponent {
}
@Component({
template: `
<h3 class="title">FirstComponent</h3>
<nav>
</nav>
<hr />
<router-outlet></router-outlet>
`
})
export class FirstComponent {
}
@Component({
template: `
<h3 class="title">SecondComponent</h3>
<nav>
<a routerLink="second" routerLinkActive="active" >Second</a>
<a routerLink="third" routerLinkActive="active" >Third</a>
</nav>
<hr />
<router-outlet></router-outlet>
`
})
export class SecondComponent {
}
@Component({
template: `
<h3 class="title">ThirdComponent</h3>
`
})
export class ThirdComponent {
}
@Component({
template: `
<h3 class="title">FourthComponent</h3>
<hr />
`
})
export class FourthComponent {
}
Upvotes: 3