如何设置Angular 2本地存储超时

时间:2018-05-09 16:54:46

标签: javascript angular session

我有一个带有登录和“会话”变量的Angular 2/5项目。我需要 像本地存储上的.Net会话超时之类的东西。我该怎么办?

1 个答案:

答案 0 :(得分:0)

嗨,我遇到了同样的问题并以这种方式解决了:

Naudojimas:
./test failas_ar_katalogas

然后在您的组件中

// rxjs V6

import {Injectable} from "@angular/core";
import { Observable,timer, interval, of, Subject } from 'rxjs';
import { tap, map, share, switchMap, distinctUntilChanged} from rxjs/operators';
import * as moment from 'moment';

        @Injectable()
        export class ClockService {
          private interval = 1000;//1sec
          private countDown$:Observable<boolean>;
        public starter: Subject<number> = new Subject();

            public countdown(start:number):Observable<boolean>{

            this.setCountdown(start);
            return this.countDown$;

          }

        private setCountdown(_start:number){

    let start =moment().add(_start, 's').unix();

    this.countDown$= interval(this.interval).pipe(
    map(tick => start - moment().unix()),
    map(tack=>tack==0? true : false),
    distinctUntilChanged(),
    share()
    );

  }
每次请求后

  this.countService.starter.pipe(
    switchMap((start)=>this.countService.countdown(start)))
.subscirbe(tick=>tick? /* clean localSotrage */ : /* do nothing */)