Angular 2-使用ng2-idle注销 [英] Angular 2 - Logout using ng2-idle

查看:192
本文介绍了Angular 2-使用ng2-idle注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is that when click on the logout button ng2-idle continues working. And to try to solve this problem I set again the setIdle and setTimeout functions for 1 second.

但是,当用户转移到登录屏幕时,该应用需要1秒钟的超时时间.

However, when the user is transferred to the login screen, the app takes 1 second to give the timeout.

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

I like to know if have any way of forcing the timeout or end ng2-idle after click the logout button that call logout() function.

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

推荐答案

我开始工作,跟随logout()函数的更改:

I got to work, follows the change in the logout() function:

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

It is possible to use both function this.idle.stop() or this.idle.ngOnDestroy(); idle.ts

this.idle.ngOnDestroy();包括this.idle.stop()this.clearInterrupts();

这篇关于Angular 2-使用ng2-idle注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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