Angular 2 enrutador.navegar

2 minutos de lectura

Avatar de usuario de SpaceBeers
Cervezas Espaciales

Estoy tratando de navegar a una ruta en Angular 2 con una combinación de parámetros de ruta y consulta.

Aquí hay una ruta de ejemplo donde la ruta es la última parte de la ruta:

{ path: ':foo/:bar/:baz/page', component: AComponent }

Intentando vincular usando la matriz de esta manera:

this.router.navigate(['foo-content', 'bar-contents', 'baz-content', 'page'], this.params.queryParams)

No recibo ningún error y, por lo que puedo entender, esto debería funcionar.

Los documentos de Angular 2 (por el momento) tienen lo siguiente como ejemplo:

{ path: 'hero/:id', component: HeroDetailComponent }

['/hero', hero.id] // { 15 }

¿Alguien puede ver dónde me estoy equivocando? Estoy en el enrutador 3.

Avatar de usuario de Günter Zöchbauer
Günter Zöchbauer

Si el primer segmento no comienza con / es una ruta relativa. router.navigate necesita un relativeTo parámetro para la navegación relativa

O haces la ruta absoluta:

this.router.navigate(['/foo-content', 'bar-contents', 'baz-content', 'page'], this.params.queryParams)

o pasas relativeTo

this.router.navigate(['../foo-content', 'bar-contents', 'baz-content', 'page'], {queryParams: this.params.queryParams, relativeTo: this.currentActivatedRoute})

Ver también

  • Sepa que este hilo es antiguo, pero un par de preguntas rápidas para mi comprensión: 1. En el segundo caso anterior, ¿no debería ser así? este.enrutador.navegar(**['foo**-content', 'bar-contents',.... (without the / at the beginning of first segment, since it is relative) 2. Apart from this.currentActivatedRoute what are the other possible values for relativeTo?

    – Vikas

    Dec 16, 2017 at 18:22


  • If you want it to be relative, then omit the leading /, I didn’t intend that. Any route you want the path be relative to.

    – Günter Zöchbauer

    Dec 16, 2017 at 18:46

  • Thanks – but in your reply you have mentioned that if the first segment starts with a /, then it is an absolute navigation. So why would the relativeTo be required?

    – Vikas

    Dec 17, 2017 at 6:11

  • Now I see what you mean. relativeTo makes only sense with a relative route. Thanks for pointing that out.

    – Günter Zöchbauer

    Dec 17, 2017 at 7:46

yala ramesh's user avatar
yala ramesh

import { ActivatedRoute } from '@angular/router';

export class ClassName {
  
  private router = ActivatedRoute;

    constructor(r: ActivatedRoute) {
        this.router =r;
    }

onSuccess() {
     this.router.navigate(['/user_invitation'], {queryParams: {email: loginEmail, código: código de usuario}});  } } Obtenga estos valores: --------------- ngOnInit() { this.route .queryParams .subscribe(params => { let code = params['code'];  let userEmail = params['email'];  });  }

Árbitro: https://angular.io/docs/ts/latest/api/router/index/NavigationExtras-interface.html

Avatar de usuario de Shivaram Tolanur
Shivaram Tolanur

Tan simple como

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

constructor( private router:Router) {}

    return(){this.router.navigate(["https://stackoverflow.com/",'input']);}

Aquí se le redirigirá a la entrada de ruta. Si desea ir a una ruta en particular con relación a alguna ruta, entonces.

return(){this.router.navigate(['/relative','input']);}

Aquí en return() es el método que se activará al hacer clic en un botón

¿Ha sido útil esta solución?