官术网_书友最值得收藏!

Navigating between states

To add logic to the back button from here on is relatively simpler. When the user clicks on the Back button, we will navigate to the previous state of the application from the stack. If the stack was empty when the user clicks the Back button, meaning that the user is at the starting state, then we set it back into the stack because we do the pop() operation to determine the current state of the stack.

goBack() {
let current = this.stack.pop();
let prev = this.stack.peek();

if (prev) {
this.stack.pop();

// angular provides nice little method to
// transition between the states using just the url if needed.
this.router.navigateByUrl(prev.urlAfterRedirects);

} else {
this.stack.push(current);
}
}

Note here that we are using urlAfterRedirects instead of plain urlThis is because we do not care about all the hops a particular URL made before reaching its final form, so we can skip all the redirected paths that it encountered earlier and send the user directly to the final URL after the redirects. All we need is the final state to which we need to navigate our user because that's where they were before navigating to the current state.

主站蜘蛛池模板: 庄浪县| 达尔| 福州市| 四川省| 凭祥市| 罗源县| 兴海县| 罗江县| 明光市| 喀什市| 岗巴县| 金寨县| 玉溪市| 海丰县| 张家港市| 松原市| 英德市| 进贤县| 徐汇区| 永修县| 黎川县| 綦江县| 镇康县| 介休市| 南陵县| 阿图什市| 广丰县| 乐安县| 曲松县| 甘德县| 钦州市| 当雄县| 会理县| 铜川市| 道孚县| 开远市| 耒阳市| 韶山市| 牙克石市| 运城市| 塘沽区|