首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular 2-使用ng2-idle注销

Angular 2-使用ng2-idle注销
EN

Stack Overflow用户
提问于 2016-08-28 23:24:01
回答 1查看 13.8K关注 0票数 3

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

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

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

代码语言:javascript
运行
复制
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)
            )}
        )
    }  
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-29 07:01:46

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

代码语言:javascript
运行
复制
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();

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39192832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档