gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:57:10 25 4
gpt4 key购买 nike

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

详情请看代码中的注释。谢谢

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

最佳答案

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

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

关于typescript - 有子路由时如何返回上一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35927791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com