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

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.

主站蜘蛛池模板: 安新县| 固始县| 临西县| 盐山县| 德格县| 阳江市| 松阳县| 丹阳市| 林芝县| 广水市| 大名县| 礼泉县| 黑龙江省| 六盘水市| 缙云县| 庄浪县| 淮滨县| 陇南市| 五原县| 定远县| 米泉市| 通州市| 小金县| 巨鹿县| 凤城市| 安顺市| 尼勒克县| 临朐县| 綦江县| 兴海县| 花莲市| 石景山区| 农安县| 平舆县| 英山县| 揭阳市| 齐河县| 泸州市| 理塘县| 咸阳市| 田阳县|