网站搜索

typescript - 当有子路由时如何返回上一页?


当有子路由时如何返回上一页?

请参阅代码中的注释了解详情。谢谢

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}`);
  }
}

答案#1

Angular 2 关于路由的文档建议你改用 window 对象 https://angular.io/docs/ts/latest/tutorial/toh-pt5.html#!#find-our-way-back

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

答案#2

在 HTML 标签中尝试这个

 routerLink="../LASTPAGE"

尝试 routerLink="../LASTPAGE"