Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 347
OPV Avatar asked Aug 14 '17 07:08

OPV


People also ask

How do I check Angular routes?

There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service.

What would you use in Angular 2 to define routes?

Instead of “href” attribute of anchor tag, we use the “routerLink” attribute of Angular. The routerLink attribute allows us to link to a specific route of the Application.

What is RouterModule in Angular?

Adds directives and providers for in-app navigation among views defined in an application. Use the Angular Router service to declaratively specify application states and manage state transitions.


3 Answers

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.

like image 174
devqon Avatar answered Oct 13 '22 11:10

devqon


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)
  }

}
like image 43
Tom Avatar answered Oct 13 '22 09:10

Tom


In addition to devqons excellent answer: Debugging your route definitions will be much easier if you temporarily out-comment wildcard routes. Wildcard routes are handy in production to show e.g. a NotFound component, but are a pain while debugging.

For example:

const routes: Routes = [
    ... (your route definions)

    // If you have a catch-all route defined, outcomment is like below
    // {
    //     path: '**',
    //     redirectTo: '/error/not-found',
    // },
];

After outcommenting your catch-all route, the router will not swallow your error and show in your browser console exactly what route could not be matched against your definitions.

For example, when the following error is shown:

core.js:4002 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'projects/123'
Error: Cannot match any routes. URL Segment: 'projects/123'
    at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError (router.js:2459)

you immediately known that there is a problem with matching 'projects/123' in your route definitions.

like image 4
Wouter van Koppen Avatar answered Oct 13 '22 10:10

Wouter van Koppen