ANG_FRONT

Capítulo 8.4. Navegación con el objeto Router en Angular

Objetivo de la práctica

Duración aproximada

Instrucciones


import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html'
})
export class ExampleComponent {
  constructor(private router: Router) {}
}

goToAbout() { this.router.navigate([‘/about’]); }


- *Navegación con Parámetros*

- Si necesitas navegar a una ruta que incluye parámetros, puedes pasarlos como un array:

```typescript

goToUserDetail(userId: number) {
  this.router.navigate(['/user', userId]);
}

goToProducts(category: string) {
  this.router.navigate(['/products'], { queryParams: { category } });
}

if (isAuthenticated) {
  this.router.navigate(['/dashboard']);
} else {
  this.router.navigate(['/login']);
}

import { Location } from '@angular/common';

constructor(private location: Location) {}

goBack() {
  this.location.back();
}

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  constructor(private router: Router) {}

  login() {
    // Lógica de autenticación
    this.router.navigate(['/dashboard']);
  }

  logout() {
    this.router.navigate(['/login']);
  }
}