Angular 如何使用NgIdleKeepaliveModule防止构造函数中登录组件的会话超时

Angular 如何使用NgIdleKeepaliveModule防止构造函数中登录组件的会话超时,angular,Angular,我在app.component.ts的ngOnInit()中调用超时会话方法,路由器条件不等于登录,但失败。问题是登录页面中出现超时会话,无法执行idle.stop() 这是我的密码: ngOnInit() { this.subscription=this._router.events .filter((e => e instanceof ActivationEnd)).subscribe((e) => { if(this._router.url.indexOf

我在
app.component.ts
ngOnInit()
中调用超时会话方法,路由器条件不等于登录,但失败。问题是登录页面中出现超时会话,无法执行
idle.stop()

这是我的密码:

ngOnInit()
{
  this.subscription=this._router.events
  .filter((e => e instanceof ActivationEnd)).subscribe((e) => 
  {
    if(this._router.url.indexOf('login')<0){
      this.sessionTimeout(this._router,this.idle,this.keepalive,this.ngbModal);
    }
  });
}

sessionTimeout( _router:Router,idle: Idle,  keepalive: Keepalive,ngbModal: NgbModal) {

  // method here
}
ngOnInit()
{
this.subscription=this.\u router.events
.filter((e=>e instanceof activationnd)).subscribe((e)=>
{

如果(this._router.url.indexOf('login'),您可以在构造函数中检查路由器url条件,这将防止登录组件中的会话超时

如果您试图在app.component.ts中实现此内部构造函数,下面的代码可能会对您有所帮助

constructor(private router:Router,private element: ElementRef, private idle: Idle, 
            private keepalive: Keepalive, private ngbModal: NgbModal)
  {
    idle.setIdle(900);
    idle.setTimeout(300);
    idle.setInterrupts([new EventTargetInterruptSource(
        this.element.nativeElement, 'keydown DOMMouseScroll mousewheel mousedown 
        touchstart touchmove scroll')]);

    idle.onIdleEnd.subscribe(() => {
      this.idleState = 'NO_LONGER_IDLE';
    });

    idle.onTimeout.subscribe(() => {
      this.idleState = 'TIMED_OUT';
      this.timedOut = true;
    });

    idle.onIdleStart.subscribe(() => {
      this.idleState = 'IDLE_START'
    });

    idle.onTimeoutWarning.subscribe((countdown: any) => {
      //subscribe code here
    });
    keepalive.interval(15);

  //to prevent login component from timeout session
  router.events.subscribe((val) => {
    if(this.router.url.search(/login/) == -1){
        idle.watch();
       }
    else{
        idle.stop();
    }
   });  
    this.reset();
 }

在构造函数中添加路由器条件对我来说很有效。