角度 6 ng-空闲

2023-12-03

我有一个运行良好的 Angular 项目,我正在实施 NG-IDLE 和 KeepAlive,以便保持会话新鲜并在 API 会话过期之前注销用户。

我的问题是 ng-idle 也在登录页面上运行,这显然不是必需的,因为当它超时时,它会将用户带到登录页面。

因此,我在 app.component.ts 中启动并运行了 ng-idle 和 KeepAlive,但由于我使用延迟加载,因此我还有一个authentication.module.ts 和一个login.component.ts。

我的根app.component.ts中的代码如下:

import { Component } from '@angular/core';

import { Idle, DEFAULT_INTERRUPTSOURCES } from '@ng-idle/core';
import { Keepalive } from '@ng-idle/keepalive';

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

export class AppComponent {

    idleState = 'Not started.';
    timedOut = false;
    lastPing?: Date = null;

    constructor(private idle: Idle, private keepalive: Keepalive) {

        // sets an idle timeout of 5 seconds, for testing purposes.
        idle.setIdle(5);

        // sets a timeout period of 5 seconds. after 10 seconds of inactivity, the user will be considered timed out.
        idle.setTimeout(5);

        // sets the default interrupts, in this case, things like clicks, scrolls, touches to the document
        idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);

        idle.onIdleEnd.subscribe(() => this.idleState = 'No longer idle.');

        idle.onTimeout.subscribe(() => {
            this.idleState = 'Timed out!';
            this.timedOut = true;
        });

        idle.onIdleStart.subscribe(() => this.idleState = 'You\'ve gone idle!');
        idle.onTimeoutWarning.subscribe((countdown) => this.idleState = 'You will time out in ' + countdown + ' seconds!');

        // Sets the ping interval to 15 seconds
        keepalive.interval(15);

        keepalive.onPing.subscribe(() => this.lastPing = new Date());

        this.reset();
    }

    reset() {
        this.idle.watch();
        this.idleState = 'Started.';
        this.timedOut = false;
    }
}

我知道我需要调用idle.unwatch以防止空闲运行,并且在需要时调用idle.watch,但是我如何从登录或身份验证模块调用这些,或者我可以从根app.component检测。 TS?

毫无疑问,你可以看出我是 Angular 的新手,所以如果这是一个菜鸟问题,我深表歉意。


由于给猫剥皮的方法总是不止一种,所以这是我自己解决这个问题的方法。我希望其他人将来会发现它有用。

import { Component } from '@angular/core';

import { Location } from '@angular/common';
import { Router } from '@angular/router';

import { Idle, DEFAULT_INTERRUPTSOURCES } from '@ng-idle/core';
import { Keepalive } from '@ng-idle/keepalive';

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

export class AppComponent {

    currentPath: String;

    idleState = 'Not started.';
    timedOut = false;
    lastPing?: Date = null;

    constructor(private idle: Idle, private keepalive: Keepalive, location: Location, router: Router) {

        // sets an idle timeout of 5 seconds, for testing purposes.
        idle.setIdle(5);

        // sets a timeout period of 5 seconds. after 10 seconds of inactivity, the user will be considered timed out.
        idle.setTimeout(5);

        // sets the default interrupts, in this case, things like clicks, scrolls, touches to the document
        idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);

        idle.onIdleEnd.subscribe(() => this.idleState = 'No longer idle.');

        idle.onTimeout.subscribe(() => {
            this.idleState = 'Timed out!';
            this.timedOut = true;
        });

        idle.onIdleStart.subscribe(() => this.idleState = 'You\'ve gone idle!');
        idle.onTimeoutWarning.subscribe((countdown) => this.idleState = 'You will time out in ' + countdown + ' seconds!');

        // Sets the ping interval to 15 seconds
        keepalive.interval(15);

        keepalive.onPing.subscribe(() => this.lastPing = new Date());

        // Lets check the path everytime the route changes, stop or start the idle check as appropriate.
        router.events.subscribe((val) => {

            this.currentPath = location.path();
            if(this.currentPath.search(/authentication\/login/gi) == -1)
                idle.watch();
            else
                idle.stop();

        });
    }

    reset() {
        this.idle.watch();
        this.idleState = 'Started.';
        this.timedOut = false;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

角度 6 ng-空闲 的相关文章

随机推荐

  • 尝试将 XInclude 与 Java 结合使用并使用 xml:id 解析片段

    我一直在尝试让 XInclude 在我的 XML 文档中工作 最后让它在 Oxygen XML 中工作 我用 Oxygen XML 来编写 XML 文档 然后我转到我的应用程序 用 Java 编写 但它似乎不支持任何形式的 XPointer
  • 将 pandas 中的列表拆分为列

    我有一个像这样的数据框 df pd DataFrame ID ID1 ID2 ID3 Values AB BC np NaN AB CD df ID Values 0 ID1 AB BC 1 ID2 NaN 2 ID3 AB CD 我想将列
  • MssqlHook气流连接

    我是使用气流的新手 我需要做的是使用 MssqlHook 但我不知道如何使用 我应该在构造函数中给出哪些元素 我在气流中有一个名为connection test的连接 我不完全理解类中的属性 class MsSqlHook DbApiHoo
  • 计算多个数据集的日、月和年平均值

    我有一个数据框 MS NR SS NR DATE HOUR VALUE 1 13095010 68 1 01 2014 0 00 00 9 8 2 13095010 68 1 01 2014 1 00 00 8 0 3 13095010 6
  • css3背景大小:覆盖在调整大小时留下空白

    body margin 0 padding 0 position relative background url images imgs backgrnd png no repeat background size cover The ba
  • 用逗号分割字符串但忽略双引号内的逗号?

    我有一些如下所示的输入 A B C D12121 E F G H I9 I8 J K 逗号分隔的值可以按任何顺序 我想用逗号分隔字符串 但是 如果双引号内有某些内容 我需要它忽略逗号并删除引号 所以基本上 输出将是这个字符串列表 A B C
  • tkinter:将多个按钮 lambda 映射到一个标签?

    描述 我希望能够单击一个按钮并将其值发送到标签小部件 lab 1 我知道要执行一个按钮 您必须使用command lambda lab 1 configure text 0 但是当我使用了一个标签时 你会如何将值打印到标签上for循环在嵌入
  • 如何在iOS中的prepareForSegue之前执行按钮按下动作?

    我正在创建一个 iOS 应用程序 在运行方法prepareForSegue 之前 我想在按下按钮时执行分配操作 我使用主要故事板创建了所有控件 某些按钮的执行顺序是 按钮按下动作 gt prepareForSegue 但对于某些按钮来说是
  • Asp.net cookieless sessionId url 位置

    我正在开发一个移动网站 我们不能依靠手机访问该网站来获得 cookie 支持 我正在对会话使用 cookieless 选项 想知道是否有办法指定 sessionId 在 URL 中的放置位置 现在看起来是这样的 http www somes
  • NASM:远程调用,段和偏移量存储在寄存器中

    我已经将代码段和偏移值存储在两个寄存器中 比如说AX and BX分别 在 NASM 中 我如何编码远程调用AX BX 我试过call AX BX 但我得到了错误invalid combination of opcode and opera
  • 用 Pandas 数据框中尽可能少的日期填充缺失的日期值

    我有一个带有日期列的数据框 df pd DataFrame date 2014 10 01 np nan 2015 09 30 np nan np nan 2019 06 03 现在我想用 pandas 中尽可能少的日期值来估算缺失的日期值
  • 在 Spritekit 中取消暂停视图时,fps 显着下降

    当在 SpriteKit 中取消暂停视图时 我注意到 fps 显着下降 帧速率下降在 5 10fps 之间 我用空项目 Spritekit 游戏模板 尝试了这个 这是代码 if self view paused self view paus
  • 设置运输跟踪号码后以编程方式发送电子邮件

    我正在寻找一种在将跟踪号码分配给订单时以编程方式向用户发送电子邮件的方法 我需要能够以编程方式执行此操作 因为我正在使用外部源来填充跟踪信息 我想我在这里真正寻找的是一个特定的触发器或事件 我可以用它来触发通常在管理员单击 发送跟踪信息 按
  • PHP 警告:mysqli_connect(): (HY000/2002):连接被拒绝

    我正在使用 PHP 5 5 和 MAMP 从下载here 我有一个像这样的基本脚本 当我运行这个脚本时 我收到此错误 PHP Warning
  • 变量“a”的范围是什么?

    可能的重复 Python变量作用域问题 Python 手册将范围定义为 范围定义了块内名称的可见性 如果是本地人 变量在块中定义 其范围包括该块 如果 定义发生在功能块中 范围扩展到任何块 包含在定义块中 除非包含块引入 名称的不同绑定 我
  • 无法使编辑功能正常工作

    我有两个型号Employee and Overtime Definition协会是这样设置的 Employee class Employee lt ActiveRecord Base has many overtime definition
  • 在 Excel 中查询不带标题 - Oracle

    是否可以将数据导出到不带标题的 Excel 工作表中 我知道 SET HEAD OFF 在 SQL Developer 中有效 例子 出发 选择数量 客户 来自客户 但是 如果我使用 Microsoft Query 向导和 ODBC 如何在
  • 在集合视图中为 UIswitch 编写事件

    您好 我正在尝试在 UI 集合视图中编写 UI 切换和 UI 分段控制的事件 我在集合视图单元格中声明了 UIswitch 和 UIsegmentation 控件 interface CollectionViewCell UICollect
  • 如何使用 REGEX 将文本拆分为块,并在特定字符上断开?

    我希望将长文本分成最多 1000 个字符的块 为了在每个块中获取尽可能多的字符 但重要的是我想在换行符中完成每个块 以避免中间的单词分割 如果所有 1000 个字符中没有一个换行符 那么我的正则表达式仍然会捕获一个单词 并将其拆分为 2 个
  • 角度 6 ng-空闲

    我有一个运行良好的 Angular 项目 我正在实施 NG IDLE 和 KeepAlive 以便保持会话新鲜并在 API 会话过期之前注销用户 我的问题是 ng idle 也在登录页面上运行 这显然不是必需的 因为当它超时时 它会将用户带