多次调用angular2路由防护 [英] Calling angular2 route guard multiple times

查看:82
本文介绍了多次调用angular2路由防护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一条私人路线上有一个路线警卫,当警卫返回false时,就不会再调用它了.下面的示例简化了如何重现这种情况.

I have an route guard on one of my private routes and when guard returns false it is not called again anymore. The following example is simplified how to reproduce such situation.

当用户使用链接导航时:

When user navigates using link:

<a [routerLink]="['my-private-route']">Link</a>

警卫被呼叫,我在控制台中看到called消息,导航被取消(我在警卫中返回false):

the guard gets called and I see called message in the console and navigation is canceled (I return false in guard):

@Injectable()
export class CanActivateViaAuthGuard implements CanActivate {
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean {
    console.log('called')
    return false;
  }
}

但是,当用户再次单击链接时,没有任何反应.

However when user clicks on the link again nothing happens.

为什么需要这样的行为?之所以需要反复调用警惕是因为我必须显示模式Do you want to login? YES/NO.当用户单击no时,我要保留在页面上.但是当他以后决定再次导航时,我想让他再次登录.

Why I need such behaviour? The reason I need guard to be called repeatedly is because I have to display modal Do you want to login? YES/NO. When user clicks no I want to stay on the page. But when he laters decides to navigate again I want to let him login again.

有什么方法可以停止angular2缓存保护结果吗?

Is there any way to stop angular2 caching guard results?

推荐答案

经过@GünterZöchbauer的调查和帮助,这是角度路由器3.2.0的奇怪行为.已经存在类似的问题: https://github.com/angular/angular/issues/12851

After some investigation and help of @GünterZöchbauer it's a strange behaviour of angular router 3.2.0. There is already similar issue: https://github.com/angular/angular/issues/12851

Here is plunker with 3.1.2 where guard is evaluated every time:

https://plnkr.co/edit/jUhVJY?p=preview

Here is plunker with 3.2.0 where guard is evaluated only once:

https://plnkr.co/edit/2A0Wfu?p=preview

这篇关于多次调用angular2路由防护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆