2016-08-28 5 views
1

ログアウトボタンをクリックすると、ng2-idleが動作し続けるのが問題です。 この問題を解決するために、setIdle関数とsetTimeout関数を1秒間再設定します。角度2 - ng2-idleを使用したログアウト

しかし、ユーザーがログイン画面に転送されると、アプリはタイムアウトを得るのに1秒かかります。

logout()関数を呼び出すログアウトボタンをクリックした後に、タイムアウトを強制する方法やng2-idleを終了する方法があるかどうかを知りたい。

logout() { 
    this.idle.stop(); 
    this._headerService.exit() 
     .subscribe(
      data => { 
       localStorage.clear(); 
       this._router.navigate(['/auth']);   
      }, 
       error => console.log(error) 
     )       
    } 

this.idle.stop()またはthis.idle.ngOnDestroy();idle.ts

this.idle.ngOnDestroy();this.idle.stop()this.clearInterrupts();含まれて両方の機能を使用することが可能である:

import {Component} from '@angular/core'; 
import {Router} from "@angular/router"; 
import {Http, Headers} from "@angular/http"; 
import {NgClass} from '@angular/common'; 
import {Observable} from "rxjs/Observable"; 
import 'rxjs/Rx'; 
import {HeaderService} from './header.service'; 
import {Idle, DEFAULT_INTERRUPTSOURCES} from 'ng2-idle/core'; 

@Component({ 
    selector: 'my-header', 
    templateUrl: './js/app/header/header.component.html', 
    styleUrls: ['./js/app/header/header.component.css'] 
}) 

export class HeaderComponent { 
    nome = localStorage['nome']; 
    constructor(private _router: Router, private _http: Http, private _headerService: HeaderService, private idle: Idle) { 
     idle.setIdle(5); 
     idle.setTimeout(1800); 
     idle.setInterrupts(DEFAULT_INTERRUPTSOURCES); 

     idle.onTimeoutWarning.subscribe((countdown:number) => { 
      console.log('TimeoutWarning: ' + countdown); 
     }); 

     idle.onTimeout.subscribe(() => { 
      console.log('Timeout'); 
      localStorage.clear(); 
      this._router.navigate(['/auth', {sessionExpirate: 'true'}]); 
     }); 
     idle.watch(); 
    } 

    logout() { 
     this.idle.setIdle(1); 
     this.idle.setTimeout(1); 
     this.idle.setInterrupts(DEFAULT_INTERRUPTSOURCES); 
     this._headerService.exit() 
      .subscribe(
       data => { 
        this.idle.onTimeout.subscribe(() => { 
         console.log('Timeout'); 
         localStorage.clear(); 
         this._router.navigate(['/auth']); 
       }, 
       error => console.log(error) 
      )} 
     ) 
    } 
} 

答えて

2
私が動作するようになった

は、ログアウト()関数の変化を次の

+0

はまだアクティブなng2-idleプロジェクトですか?あなたは私にリンクを送ってもらえますか? –

+0

私はそう信じています。サイトはhttps://www.npmjs.com/package/@ng-idle/coreにもあります。https://github.com/HackedByChinese/ng2-idle – rafaelcb21

関連する問題