【问题标题】:Angular 2 - Logout using ng2-idleAngular 2 - 使用 ng2-idle 注销
【发布时间】:2017-01-04 16:37:31
【问题描述】:

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

但是,当用户被转移到登录屏幕时,应用程序需要 1 秒才能给出超时。

我想知道在单击调用 logout() 函数的注销按钮后是否有任何方法强制超时或结束 ng2-idle。

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)
            )}
        )
    }  
}

【问题讨论】:

    标签: angular logout ng-idle


    【解决方案1】:

    我开始工作了,跟着 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();

    【讨论】:

    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2018-07-27
    • 2017-05-05
    相关资源
    最近更新 更多