Búsqueda de sitios web

typescript - ¿Cómo volver a la página anterior teniendo un router hijo?


¿Cómo volver a la página anterior cuando se tiene un router hijo?

Por favor, vea los comentarios en el código para más detalles. Gracias

export class OneProductComponent {
  prevUrlPath:string = '';

  constructor(private _router: Router) {}

  routerOnActivate(next:ComponentInstruction, prev:ComponentInstruction) {
    this.prevUrlPath = prev.urlPath;
  }

  goBack() {
    // I have to manually add "products/" here, but if I need 
    // go back to root home page. Then I shouldn't add "products/" here.
    // Is there a smart way to to this?
    this._router.navigateByUrl(`products/${this.prevUrlPath}`);
  }
}

Respuesta #1

La documentación de Angular 2 sobre enrutamiento sugiere que en su lugar uses el objeto window https://angular.io/docs/ts/latest/tutorial/toh-pt5.html#!#find-our-way-back

goBack() {
  window.history.back();
}

Respuesta #2

prueba esto en la etiqueta HTML

 routerLink="../LASTPAGE"

intenta routerLink="../LASTPAGE"