代码之家  ›  专栏  ›  技术社区  ›  rafaelcb21

Angular 2-使用ng2空闲注销

  •  3
  • rafaelcb21  · 技术社区  · 8 年前

    我的问题是,当单击注销按钮ng2 idle时,它会继续工作。 为了解决这个问题,我再次设置setIdle和setTimeout函数1秒。

    然而,当用户被转移到登录屏幕时,应用程序需要1秒来给出超时。

    我想知道,在单击调用logout()函数的logout按钮后,是否有任何方法可以强制超时或结束ng2空闲。

    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)
                )}
            )
        }  
    }
    
    1 回复  |  直到 8 年前
        1
  •  6
  •   rafaelcb21    8 年前

    我开始工作,遵循logout()函数中的更改:

    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();