DEV Community

Manthan Ankolekar
Manthan Ankolekar

Posted on

11 1

Router Debugging in Angular

  1. Enable router tracing: In app.module.ts

    import { RouterModule, Routes } from '@angular/router';
    
    const routes: Routes = [
    // Define your routes here
    ];
    
    @NgModule({
    imports: [
        RouterModule.forRoot(routes, { enableTracing: true })
    ],
    // ...
    })
    export class AppModule { }
    

    if working with standalone application

    const appRoutes: Routes = [];
    bootstrapApplication(AppComponent,
    {
        providers: [
        provideRouter(appRoutes, withDebugTracing())
        ]
    }
    );
    
  2. Use console.log() to inspect

    import { Component } from '@angular/core';
    import { Router } from '@angular/router';
    
    @Component({
    // ...
    })
    export class MyComponent {
    constructor(private router: Router) {}
    
    navigateToRoute() {
        console.log('Before navigation');
        this.router.navigate(['/my-route']);
        console.log('After navigation');
    }
    }
    

    else use below code

    isLoading$ = new BehaviorSubject<boolean>(false);
    
    constructor(private router: Router) {
    this.router.events
        .subscribe((event) => {
        if (event instanceof NavigationStart) {
            this.isLoading$.next(true);    
        }
        else if (event instanceof NavigationEnd) {
            this.isLoading$.next(false); 
        }
        });
    }
    

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post