Angular2, relative navigation (two levels back ../../)

别来无恙 提交于 2019-12-12 18:16:55

问题


I have a back button in my application, which navigates relatively some levels back. In case of one level

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

it works perfectly (resulted route url:http://localhost:3000/three/two/one). In case of two level back rel. navigation

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

router navigates two levels back, BUT the resulted route url looks now like http://localhost:3000/three/two/(tailing slash, which is not correct).

Am I doing something wrong or could it be a bug?


回答1:


In case of two levels down use this:

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



回答2:


Use the Location.back() method instead, Location.back() method takes you to the last page in your browsers history.

in your component:

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

Now create a back button in your component.html and bind this method to it:

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


来源:https://stackoverflow.com/questions/43994297/angular2-relative-navigation-two-levels-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!