How to get rid from @routes directive?

Updated: Jan 27, 2025

How to get rid from @routes directive?

In order to get rid of the @routes directive in your Angular application, you need to remove the dependency on the angular-routing library. Here are the steps to do that:

  1. Remove the angular-routing library from your package.json file:
npm remove angular-routing
  1. Update your app.module.ts file to remove the RouterModule import and the RouterModule.forRoot() call:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  1. Remove any references to the Router and ActivatedRoute services in your components and services:
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
}
  1. Remove any routerLink directives from your HTML templates:
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
  1. If you have any route guards, remove them as well:
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';

export class AuthGuard implements CanActivate {
  constructor(private router: Router) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    if (localStorage.getItem('token')) {
      return true;
    }

    this.router.navigate(['/login']);
    return false;
  }
}

After following these steps, you should no longer have the @routes directive in your Angular application. However, keep in mind that without routing, your application will only have a single page, and you won't be able to navigate between different components. If you need multiple pages, you'll need to re-introduce routing using a different library or approach, such as Angular's built-in RouterModule or a client-side routing library like ngrx-router-store.