Angular2,相对导航(两级后退../../)

时间:2017-05-16 06:49:24

标签: angular routing navigation

我的应用程序中有一个后退按钮,可以在相对较高的水平上导航。如果是一个级别

this.router.navigate(['..'], { relativeTo: this.route });

它完美运行(结果路线网址:http://localhost:3000/three/two/one)。如果是两级后退。导航

this.router.navigate(['..', '..'], { relativeTo: this.route });

路由器导航两个级别,但是结果路径网址现在看起来像http://localhost:3000/three/two/(拖尾斜线,这是不正确的)。

我做错了什么或者它可能是一个错误?

2 个答案:

答案 0 :(得分:3)

如果两级关闭,请使用此选项:

this.router.navigate(['../../'], { relativeTo: this.route });

答案 1 :(得分:0)

使用Location.back()方法,Location.back()方法将您带到浏览器历史记录的最后一页。

组件中的

import { Location }  from '@angular/common';
//some othe code ......
//in your class
constructor(private location: Location) {}

//create a method to go back
goBack() {
  this.location.back();
}

现在在component.html中创建一个后退按钮并将此方法绑定到它:

<button (click)="goBack()" > Back </button>