CanDeactivate 更改窗口历史记录

Dee*_*rma 5 angular2-routing

每当 canDeactivate() 返回 false 时,我都会遇到一些问题,它会更改窗口历史记录,因为它变为 true 并且如果我按下后退按钮。我正在导航到其他一些 URL 或应用程序本身之外。

请帮我

Val*_*kov 8

这里是问题,但它仍然没有解决。

作为一种解决方法,您可以手动将活动 url 放回历史记录:

export class CanDeactivateGuard implements CanDeactivate<any> {
    constructor(
        private readonly location: Location,
        private readonly router: Router
    ) {}

    canDeactivate(component: any, currentRoute: ActivatedRouteSnapshot): boolean {
        if (myCondition) {
            const currentUrlTree = this.router.createUrlTree([], currentRoute);
            const currentUrl = currentUrlTree.toString();
            this.location.go(currentUrl);
            return false;
        } else {
            return true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)