我终于想出了怎么做 import { Events } from 'ionic-angular'; import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import 'rxjs/add/observable/interval'; import 'rxjs/add/operator/mapTo'; import 'rxjs/add/observable/defer'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/merge'; import 'rxjs/add/operator/scan'; import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/switchMap'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; export enum AuthState { LOGGED_IN, LOGGED_OUT } const SESSION_TIMEOUT = 10; // seconds export class AuthService { private timer: number; private tick$: Observable<number>; private countDown$: Observable<number>; private subscriptions:Subscription[]; constructor( private events:Events ) { this.tick$ =Observable.interval(1000).mapTo(-1); this.countDown$ = Observable .defer(() => Observable.of(this.timer)) .merge(this.tick$) .scan((acc,curr) => acc + curr) this.events.subscribe(RESET_COUNTDOWN, (action:string) => { this.resetSessionTimer(action); }); } beginCountDown() { let sub:Subscription; while (this.subscriptions.length > 0) { sub = this.subscriptions.pop(); sub.unsubscribe(); }; this.timer = SESSION_TIMEOUT; sub = this.countDown$ .filter(timer => timer >= 0) .subscribe(timer => {this.timer = timer; if (this.timer === 1) { this.logout(); } console.log(this.timer); }); this.subscriptions.push(sub); } resetSessionTimer(action:string) { console.log('AuthService: resetSessionTimer(): ' + action); this.beginCountDown(); } emitAuthState():void { this.authManager.next(this.authState); } private setAuthState(newAuthState:AuthState):void { if (newAuthState != this.authState) { this.authState = newAuthState; this.emitAuthState(); } } login( clickStream$: BehaviorSubject<any> ) { this.response$ = clickStream$.switchMap(click => { return this.webServiceUtil.testLogin(); } ); this.response$.subscribe((response:Response) => { this.setAuthState(AuthState.LOGGED_IN); }, (err:any) => { this.setAuthState(AuthState.LOGGED_OUT); }) } logout() { if (this.authState === AuthState.LOGGED_IN) { this.setAuthState(AuthState.LOGGED_OUT); } } } 从“离子角度”导入{Events}; 从“rxjs/Observable”导入{Observable}; 从'rxjs/Subscription'导入{Subscription}; 导入“rxjs/add/observable/interval”; 导入'rxjs/add/operator/mapTo'; 导入“rxjs/add/observable/defer”; 导入“rxjs/add/observable/of”; 导入“rxjs/add/operator/merge”; 导入“rxjs/add/operator/scan”; 导入'rxjs/add/operator/filter'; 导入'rxjs/add/operator/switchMap'; 从'rxjs/BehaviorSubject'导入{BehaviorSubject}; 导出枚举身份验证状态{ 登录, 注销 } const SESSION_TIMEOUT=10;//秒 导出类身份验证服务{ 私人定时器:数字; 私人勾号$:可见; 私人倒计时$:可观察; 私人订阅:订阅[]; 建造师( 私人活动:活动 ) { 此.tick$=可观察的.interval(1000).mapTo(-1); 此。倒计时$=可观察 .defer(()=>可观察到的(这个计时器)) .merge(此.tick$) .扫描((acc,curr)=>acc+curr) this.events.subscribe(重置\u倒计时,(操作:字符串)=>{ 这是resetSessionTimer(操作); }); } 开始计数(){ 让子:订阅; 而(this.subscriptions.length>0){ sub=this.subscriptions.pop(); sub.取消订阅(); }; this.timer=会话超时; sub=this.countDown$ .filter(计时器=>计时器>=0) .订阅(计时器=> {this.timer=计时器; 如果(this.timer==1){ 此参数为.logout(); } console.log(this.timer); }); 这个.subscriptions.push(sub); } resetSessionTimer(操作:字符串){ log('AuthService:resetSessionTimer():'+操作); 这个。beginCountDown(); } emitAuthState():void{ this.authManager.next(this.authState); } 私有setAuthState(newAuthState:AuthState):无效{ if(newAuthState!=此.authState){ this.authState=newAuthState; this.emitAuthState(); } } 登录( 单击流$:行为主题 ) { 此.response$=clickStream$.switchMap(单击=>{ 返回此.webServiceUtil.testLogin(); } ); this.response$.subscribe((response:response)=>{ this.setAuthState(AuthState.LOGGED_); }, (错误:任意)=>{ this.setAuthState(AuthState.LOGGED_); }) } 注销(){ if(this.authState===authState.LOGGED\u IN){ this.setAuthState(AuthState.LOGGED_); } } }

我终于想出了怎么做 import { Events } from 'ionic-angular'; import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import 'rxjs/add/observable/interval'; import 'rxjs/add/operator/mapTo'; import 'rxjs/add/observable/defer'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/merge'; import 'rxjs/add/operator/scan'; import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/switchMap'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; export enum AuthState { LOGGED_IN, LOGGED_OUT } const SESSION_TIMEOUT = 10; // seconds export class AuthService { private timer: number; private tick$: Observable<number>; private countDown$: Observable<number>; private subscriptions:Subscription[]; constructor( private events:Events ) { this.tick$ =Observable.interval(1000).mapTo(-1); this.countDown$ = Observable .defer(() => Observable.of(this.timer)) .merge(this.tick$) .scan((acc,curr) => acc + curr) this.events.subscribe(RESET_COUNTDOWN, (action:string) => { this.resetSessionTimer(action); }); } beginCountDown() { let sub:Subscription; while (this.subscriptions.length > 0) { sub = this.subscriptions.pop(); sub.unsubscribe(); }; this.timer = SESSION_TIMEOUT; sub = this.countDown$ .filter(timer => timer >= 0) .subscribe(timer => {this.timer = timer; if (this.timer === 1) { this.logout(); } console.log(this.timer); }); this.subscriptions.push(sub); } resetSessionTimer(action:string) { console.log('AuthService: resetSessionTimer(): ' + action); this.beginCountDown(); } emitAuthState():void { this.authManager.next(this.authState); } private setAuthState(newAuthState:AuthState):void { if (newAuthState != this.authState) { this.authState = newAuthState; this.emitAuthState(); } } login( clickStream$: BehaviorSubject<any> ) { this.response$ = clickStream$.switchMap(click => { return this.webServiceUtil.testLogin(); } ); this.response$.subscribe((response:Response) => { this.setAuthState(AuthState.LOGGED_IN); }, (err:any) => { this.setAuthState(AuthState.LOGGED_OUT); }) } logout() { if (this.authState === AuthState.LOGGED_IN) { this.setAuthState(AuthState.LOGGED_OUT); } } } 从“离子角度”导入{Events}; 从“rxjs/Observable”导入{Observable}; 从'rxjs/Subscription'导入{Subscription}; 导入“rxjs/add/observable/interval”; 导入'rxjs/add/operator/mapTo'; 导入“rxjs/add/observable/defer”; 导入“rxjs/add/observable/of”; 导入“rxjs/add/operator/merge”; 导入“rxjs/add/operator/scan”; 导入'rxjs/add/operator/filter'; 导入'rxjs/add/operator/switchMap'; 从'rxjs/BehaviorSubject'导入{BehaviorSubject}; 导出枚举身份验证状态{ 登录, 注销 } const SESSION_TIMEOUT=10;//秒 导出类身份验证服务{ 私人定时器:数字; 私人勾号$:可见; 私人倒计时$:可观察; 私人订阅:订阅[]; 建造师( 私人活动:活动 ) { 此.tick$=可观察的.interval(1000).mapTo(-1); 此。倒计时$=可观察 .defer(()=>可观察到的(这个计时器)) .merge(此.tick$) .扫描((acc,curr)=>acc+curr) this.events.subscribe(重置\u倒计时,(操作:字符串)=>{ 这是resetSessionTimer(操作); }); } 开始计数(){ 让子:订阅; 而(this.subscriptions.length>0){ sub=this.subscriptions.pop(); sub.取消订阅(); }; this.timer=会话超时; sub=this.countDown$ .filter(计时器=>计时器>=0) .订阅(计时器=> {this.timer=计时器; 如果(this.timer==1){ 此参数为.logout(); } console.log(this.timer); }); 这个.subscriptions.push(sub); } resetSessionTimer(操作:字符串){ log('AuthService:resetSessionTimer():'+操作); 这个。beginCountDown(); } emitAuthState():void{ this.authManager.next(this.authState); } 私有setAuthState(newAuthState:AuthState):无效{ if(newAuthState!=此.authState){ this.authState=newAuthState; this.emitAuthState(); } } 登录( 单击流$:行为主题 ) { 此.response$=clickStream$.switchMap(单击=>{ 返回此.webServiceUtil.testLogin(); } ); this.response$.subscribe((response:response)=>{ this.setAuthState(AuthState.LOGGED_); }, (错误:任意)=>{ this.setAuthState(AuthState.LOGGED_); }) } 注销(){ if(this.authState===authState.LOGGED\u IN){ this.setAuthState(AuthState.LOGGED_); } } },angular,typescript,rxjs,ionic3,Angular,Typescript,Rxjs,Ionic3,重置倒计时事件可能会在页面更改或执行HTTP请求时触发。这将迫使倒计时重新开始,同时只有一个订阅,因此需要跟踪一系列订阅。。重置后立即取消 res .Subscribe(_ => { update.OnNext(true); Console.WriteLine("Status sent."); }); import { Injectable } from '@angular/core'; import { Obs

重置倒计时事件可能会在页面更改或执行HTTP请求时触发。这将迫使倒计时重新开始,同时只有一个订阅,因此需要跟踪一系列订阅。。重置后立即取消

res
    .Subscribe(_ =>
    {
        update.OnNext(true);
        Console.WriteLine("Status sent.");
    });
import { Injectable }           from '@angular/core';
import { Observable }           from 'rxjs/Observable';
import { BehaviorSubject }      from 'rxjs/BehaviorSubject';
import                               'rxjs/add/operator/map';
import                               'rxjs/add/operator/filter';
import                               'rxjs/add/Observable/timer';
import                               'rxjs/add/operator/do';
import                               'rxjs/add/operator/switch';
@Injectable()
export class AuthService {
  private authState:    AuthState;
  private authManager:  BehaviorSubject<AuthState>;
  public  authChange$:  Observable<AuthState>;
  private resetManager: Subject<boolean>;
  constructor() {
    this.authManager = new BehaviorSubject(AuthState.LOGGED_OUT);
    this.authChange$ = this.authManager.asObservable();
    this.authChange$
      .filter((authState:AuthState) => authState === AuthState.LOGGED_IN)
      .map(   (authState:AuthState) => Observable.timer(SESSION_TIMEOUT))
      .do(    () => 
        console.log('Logged In. Session Timout counting down from now'))
      .switch()
      .subscribe( () => {console.log('Timer ended: Logging out')
                         this.logout();
                        });
  }

  login() {
    this.setAuthState(AuthState.LOGGED_IN);
  }
  logout() {
    this.setAuthState(AuthState.LOGGED_OUT);
  }

  resetSessionTimer() {
    this.resetManager.next(true);
  } 

  emitAuthState():void {
    this.authManager.next(this.authState);
  }

  private setAuthState(newAuthState:AuthState):void {
    console.log('AuthService: setAuthState: ', 
        AuthState[newAuthState.toString()]);
    if (newAuthState != this.authState) {
      this.authState = newAuthState;
      this.emitAuthState();
    }
  }

export enum AuthState {
  LOGGED_IN,
  LOGGED_OUT
}

const SESSION_TIMEOUT = 5000;
import { Events }               from 'ionic-angular';
import { Observable }           from 'rxjs/Observable';
import { Subscription }         from 'rxjs/Subscription';
import                               'rxjs/add/observable/interval';
import                               'rxjs/add/operator/mapTo';
import                               'rxjs/add/observable/defer';
import                               'rxjs/add/observable/of';
import                               'rxjs/add/operator/merge';
import                               'rxjs/add/operator/scan';
import                               'rxjs/add/operator/filter';
import                               'rxjs/add/operator/switchMap';
import { BehaviorSubject }      from 'rxjs/BehaviorSubject';

export  enum AuthState {
  LOGGED_IN,
  LOGGED_OUT
}

const SESSION_TIMEOUT = 10; // seconds

export class AuthService {
  private timer:         number;
  private tick$:         Observable<number>;
  private countDown$:    Observable<number>;
  private subscriptions:Subscription[];

  constructor(
    private events:Events
  ) {
    this.tick$ =Observable.interval(1000).mapTo(-1);
    this.countDown$ = Observable
      .defer(() => Observable.of(this.timer))
      .merge(this.tick$)
      .scan((acc,curr) => acc + curr)
    this.events.subscribe(RESET_COUNTDOWN, (action:string) => {
      this.resetSessionTimer(action);
    });
  }

  beginCountDown() {
    let sub:Subscription;
    while (this.subscriptions.length > 0) {
      sub = this.subscriptions.pop();
      sub.unsubscribe();
    };
    this.timer = SESSION_TIMEOUT;
    sub = this.countDown$
      .filter(timer => timer >= 0)
      .subscribe(timer => 
        {this.timer = timer;
         if (this.timer === 1) {
           this.logout();
         }
         console.log(this.timer);
        });
    this.subscriptions.push(sub);
  }

  resetSessionTimer(action:string) {
    console.log('AuthService: resetSessionTimer(): ' + action);
    this.beginCountDown();
  } 

  emitAuthState():void {
    this.authManager.next(this.authState);
  }

  private setAuthState(newAuthState:AuthState):void {
    if (newAuthState != this.authState) {
      this.authState = newAuthState;
      this.emitAuthState();
    }
  }
  login(
   clickStream$: BehaviorSubject<any>
  ) {

     this.response$ = clickStream$.switchMap(click =>  {
                      return this.webServiceUtil.testLogin();
                                                      }
                                            );
     this.response$.subscribe((response:Response) => {
          this.setAuthState(AuthState.LOGGED_IN);
      },
     (err:any)          => {
         this.setAuthState(AuthState.LOGGED_OUT);  
     })
  }

  logout() {
      if (this.authState === AuthState.LOGGED_IN) {
         this.setAuthState(AuthState.LOGGED_OUT);
      }
  }
}