How do you redirect to a URL in Angular?

← Ionic answers

For a route in your app call router.navigateByUrl('/home'); for another website set window.location.href. A route-table redirectTo handles fixed redirects.

import { Component, inject } from '@angular/core';
import { Router, Routes, CanActivateFn } from '@angular/router';
 
@Component({ selector: 'app-login', template: '' })
export class LoginComponent {
  private router = inject(Router);
 
  done() {
    this.router.navigateByUrl('/home');               // inside the app
    this.router.navigate(['/items', 42], { queryParams: { tab: 'info' } });
  }
 
  openDocs() {
    window.location.href = 'https://example.com/docs'; // external site
  }
}
 
// Fixed redirect in the route table
export const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadComponent: () => import('./home.page').then(m => m.HomePage) },
];
 
// Redirect from a guard
export const authGuard: CanActivateFn = () =>
  inject(AuthService).loggedIn() || inject(Router).createUrlTree(['/login']);

In Ionic apps, NavController.navigateRoot('/home') also resets the back stack, which suits a post-login redirect.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required