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

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.

主站蜘蛛池模板: 邵阳市| 大冶市| 睢宁县| 个旧市| 烟台市| 六安市| 黎城县| 太白县| 武城县| 洪洞县| 连州市| 永年县| 伊川县| 航空| 淅川县| 屯门区| 大安市| 手游| 进贤县| 巧家县| 保亭| 丽水市| 巴东县| 赤壁市| 屯昌县| 平南县| 贵南县| 湘阴县| 隆回县| 调兵山市| 庐江县| 广灵县| 临夏市| 贵定县| 安仁县| 阜阳市| 包头市| 栾城县| 琼结县| 鲁甸县| 华容县|