ng-idle / core在浏览器上工作正常,但在控制台中引发错误

时间:2019-05-24 14:21:54

标签: ng-idle

我正在使用ng-idle / core来实现空闲会话超时弹出窗口,计数器分钟和秒可以正确显示,但是当计数器从60秒移到58秒时,会出现类似“ TypeError:Cannot set property'countMinutes'为空”,但计数器继续下降,在达到0秒后也成功注销。

我尝试将null检查放在组件实例周围,然后计数器在调用注销之间不会达到0。

            export class TimeOutInvokingComponent implements OnInit {
              timedOut = false;
              countDown: number;
              idleTimeOutdialogRef: MatDialogRef<IdleTimeOutComponent>;
              activityTimerSubscription: Subscription;

              constructor(private idle: Idle,
                public idleTimeWarnDialog: MatDialog,
                public sessionExpiredInfoDialog: MatDialog//, private appComponent: AppComponent
                ,private authService: MsalService
              ) {
                idle.setIdle(environment.idle_warning_time);
                idle.setTimeout(environment.idle_counter_time);
                idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);

                idle.onTimeout.subscribe(() => {
                  this.timedOut = true;
                  this.closeidleTimeOutPopup();
                  this.stopidleTimer();
                  this.logout();
                });
                idle.onIdleStart.subscribe(() => {
                  this.openSessionTimeOutDialog(1);
                });

                idle.onTimeoutWarning.subscribe((countdown: any) => {
                  this.countDown = countdown;

                 // if(this.idleTimeOutdialogRef.componentInstance && this.idleTimeOutdialogRef.componentInstance != null){
                    //alert(typeof this.idleTimeOutdialogRef);
                  this.idleTimeOutdialogRef.componentInstance.countMinutes = (Math.floor(countdown / 60));
                  this.idleTimeOutdialogRef.componentInstance.countSeconds = countdown % 60;
                //}
                });

                this.idle.onIdleEnd.subscribe(() => {
                  console.log('on idle end');
                });


              }

              ngOnInit() { }

              openSessionTimeOutDialog(count: number) {

                const timeOutWarndialogConfig = new MatDialogConfig();

                timeOutWarndialogConfig.disableClose = true;
                timeOutWarndialogConfig.autoFocus = true;
                timeOutWarndialogConfig.panelClass = 'custom-modalbox';

                timeOutWarndialogConfig.data = {
                  sessionExpiry: false,
                  countdown : this.countDown,
                  countMinutes : (Math.floor(this.countDown / 60)),
                  countSeconds : this.countDown % 60
                };

                this.idleTimeWarnDialog.closeAll();
                this.idleTimeOutdialogRef = this.idleTimeWarnDialog.open(
                  IdleTimeOutComponent,
                  timeOutWarndialogConfig
                );
                this.idleTimeOutdialogRef.componentInstance.countdown = count;
                this.idleTimeOutdialogRef.afterClosed().subscribe((result: any) => {
                  //console.log(result);
                  if (result !== '' && 'logout' === result) {
                    //console.log('Logout is initiate');

                    const sessionExpdialogConfig = new MatDialogConfig();

                    sessionExpdialogConfig.disableClose = true;
                    sessionExpdialogConfig.autoFocus = true;
                    sessionExpdialogConfig.panelClass = 'custom-modalbox';

                    sessionExpdialogConfig.data = {
                      sessionExpiry: true
                    };
                    this.sessionExpiredInfoDialog.closeAll();
                    const sessionExpireRef = this.sessionExpiredInfoDialog.open(IdleTimeOutComponent, sessionExpdialogConfig);

                    sessionExpireRef.afterClosed().subscribe((result: any) => {
                      if (result !== '' && 'logout' === result) {
                        this.stopidleTimer();
                        //this.appComponent.logout();
                        this.logout();
                      }
                      else {
                        this.initiateIdleTimer();
                      }
                    });
                  }
                });


              }

              initiateIdleTimer() {
                alert("Initiate idle timer");
                this.idle.watch();
                this.timedOut = false;
              }

              initiateActivityTimer() {
                this.activityTimerSubscription = interval(environment.activity_time).subscribe(val => {
                  console.log(val);
                  this.stopidleTimer();
                  //this.appComponent.logout();
                  this.logout();
                });
              }

              stopidleTimer() {
                this.idle.stop();
               }

              closeidleTimeOutPopup() {
                //console.log('closing popups');
                this.idleTimeOutdialogRef.close();
              }

              logout() {

                this.stopidleTimer();
                if(this.activityTimerSubscription){
                  this.activityTimerSubscription.unsubscribe();
                }
                this.authService.logout();

              }

            }

不确定在计数器从60变为58之后,为什么this.idleTimeOutdialogRef.componentInstance.countMinutes引发错误。我在这里缺少什么。任何线索都将不胜感激。

0 个答案:

没有答案