我正尝试不允许用户注销后使用浏览器后退按钮转到上一页。我希望在Angular 2中向用户显示“请登录以继续”之类的消息。
答案 0 :(得分:1)
创建一个名为authorization.guard.ts的新文件,并将其添加
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import {AppContextService} from './context';
@Injectable()
export class AuthorizationGuard implements CanActivate {
constructor(
private appContextService:AppContextService
){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
return this.appContextService.getAuthAdminLoggednIn();
}
}
稍后在您的主模块中导入{AuthorizationGuard}
将此添加到您的每个路由器路径中
{
path: 'dashboard',
canActivate:[AuthorizationGuard]
},
请参考此文件以获得完整授权 Refer this