How to trace routing in Angular 2?
I have component with separated file of routing settings:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Route } from '../core/route.service';
import { extract } from '../core/i18n.service';
import {CalendarThematicPlanComponent} from './calendar-thematic-plan.component';
const routes: Routes = Route.withShell([
{ path: 'calendar', component: CalendarThematicPlanComponent }
]);
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
providers: []
})
export class CalendarThematicPlanRoutingModule { }
When I typing URL address: http://localhost:4200/calendar
I am redirected to home page.
How can I trace routing in Angular 2?
Solution 1:
You can pass in a second argument with options:
imports: [
RouterModule.forRoot(
routes,
{ enableTracing: true } // <-- debugging purposes only
)
]
Angular will then log all events to the browser's console, per the documentation:
enableTracing?: boolean
When true, log all internal navigation events to the console. Use for debugging.
Solution 2:
As the comments in the most accepted answer suggest, this enableTracing
doesn't work in the forChild
method. A simple work around is to subscribe to all routing events in AppModule
like so:
export class AppModule {
constructor(
private readonly router: Router,
) {
router.events
.subscribe(console.log)
}
}