禁用angular2中的后退按钮

时间:2017-07-15 07:05:31

标签: angular

当在支付网关之后的重定向网址上时,想要禁用角度为2的后退按钮。我试图在beforeunload和onPopstate之前使用窗口事件,它们似乎都没有工作

我也试过使用window.history.forward但不起作用 请建议!

1 个答案:

答案 0 :(得分:5)

您可以在角度2中使用AuthGuard来满足您的要求。您只需要实现CanActivate并编写业务逻辑

import { Injectable } from '@angular/core';
import { Router, CanActivate, CanDeactivate } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class AuthGuard implements CanActivate {

constructor(private router: Router) { }

canActivate() {
   if (localStorage.getItem('payment')) { //key to indicate that user has visited payment gateway (you can change as per your needs)
        return false;
    }
    return true;
}


}

<强>路由

        {
            path: 'home',
            component: homeComponent,
            canActivate: [AuthGuard]
        }

希望它有所帮助!!