Mix*_*OID 8 javascript angular2-routing angular-cli
我有canActivate: [ AuthGuard ]
里面的路由器和验证AuthGuard
如何强制检查canActivate
相同的路由器URL?
例如:当前路线是/admin
,我有事件喜欢session expired
.我有会话签入,AuthGuard
但只有在我执行时才会激活此检查.navigate(...)
.如何强制canActivate
在同一地点运行?
我试过了:this.router.navigate([ this.router.url ]);
但角度检查相同的位置,什么都不做.
ps我可以找到"登录页面"或其他页面session expired event
,但我有所有重定向内部AuthGuard
,我不想在所有其他事件中重复相同的重定向,我只需要location.reload()
在Angular2路线中.
主要问题听起来像:如何强制canActivate
在当前位置重新运行警卫?
我的临时解决方案:
auth.service.ts
import { Injectable, Injector } from '@angular/core';
import { ActivatedRoute, Router, RouterStateSnapshot } from '@angular/router';
@Injectable()
export class AuthService {
constructor(private route: ActivatedRoute,
private router: Router,
private injector: Injector) {
this.forceRunAuthGuard();
}
// Dirty hack for angular2 routing recheck
private forceRunAuthGuard() {
if (this.route.root.children.length) {
// gets current route
const curr_route = this.route.root.children[ '0' ];
// gets first guard class
const AuthGuard = curr_route.snapshot.routeConfig.canActivate[ '0' ];
// injects guard
const authGuard = this.injector.get(AuthGuard);
// makes custom RouterStateSnapshot object
const routerStateSnapshot: RouterStateSnapshot = Object.assign({}, curr_route.snapshot, { url: this.router.url });
// runs canActivate
authGuard.canActivate(curr_route.snapshot, routerStateSnapshot);
}
}
}
Run Code Online (Sandbox Code Playgroud)
app.routes.ts
{ path: 'faq', canActivate: [ AuthGuard ], component: FaqComponent },
{ path: 'about', canActivate: [ AuthGuard ], component: AboutUsComponent },
{ path: 'upgrade', canActivate: [ AuthGuard ], component: UpgradeComponent },
Run Code Online (Sandbox Code Playgroud)
此代码AuthGuard
再次运行。
归档时间: |
|
查看次数: |
920 次 |
最近记录: |