简体   繁体   English

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

[英]How to set Angular 2 local storage timeout

I have an Angular 2/5 project with login and "session" variables. 我有一个包含登录和“会话”变量的Angular 2/5项目。 I need something like .Net session timeout on local storage. 我需要本地存储上的.Net会话超时之类的东西。 How can I do it? 我该怎么做?

Hi I had the same problem and solved this way: 嗨,我遇到了同样的问题,并通过以下方式解决了问题:

// 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()
    );

  }

and then in your components 然后在你的组件中

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

after each req 每个要求后

this.countService.starter.next(10*60*1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM