有孩子路由器时怎么回到最后一页?

问题描述 投票:1回答:2

有孩子路由器时怎么回到最后一页?

请参阅代码中的注释以获取详细信息谢谢

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}`);
  }
}
typescript angular angular2-routing
2个回答
5
投票

关于路由的角度2文档建议您使用窗口对象而不是https://angular.io/docs/ts/latest/tutorial/toh-pt5.html#!#find-our-way-back

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

0
投票

Angular 7.2.7的最新文档(灵感来自@ Victor的链接)基本上说使用注入的Location服务来实现同样的目的。 https://angular.io/tutorial/toh-pt5#find-the-way-back

(从样本中复制)

在模板中:

<button (click)="goBack()">go back</button>

和组件:

goBack(): void {
  this.location.back();
}
© www.soinside.com 2019 - 2024. All rights reserved.