【问题标题】:Angular 9 - ngx-cookie-service troubles with pathAngular 9 - ngx-cookie-service 路径问题
【发布时间】:2020-12-19 18:36:36
【问题描述】:

我正在使用 ngx-cookie-service 包来存储一些与我的应用程序相关的数据。 我需要将此 cookie 保存在基本路径 '/' 上,所以每次我都知道如何检索它。 我的这个 cookie 需要更新,当这种情况发生时,新的 cookie 必须存储在相同的路径中(所以在 '/' 中)。问题是 有时 当我刷新页面时,新的 cookie 会保存在新路径中,所以当我尝试使用 this.cookieService.get(cookieName, '/') 检索它时,它显然会失败。尽管我明确声明使用'/' 作为path,但还是会发生这种情况。它并不总是发生,这会导致调试更加困难。

这是我使用 cookie 的服务

const urlParamsCookieName = 'errepiuapp-url-params';

/**
 * This service keeps track of the keys used to retrieve objects from the backend.
 */
@Injectable({
  providedIn: 'root'
})
export class KeyLookupService {

  private _lookupUrlSegments = ['students', 'macro', 'lto', 'sto', 'reports'];
  private _paramsMap$: BehaviorSubject<Map<string, any>>;

  private get paramsMap() {
    return this._paramsMap$.getValue()
  }

  constructor(
    private cookieService: CookieService,
    private router: Router 
  ) {
    if (!this.router.url.includes('auth') && !this.cookieService.check(urlParamsCookieName)) {
      this.router.navigate(['/auth']);
    }
    this._paramsMap$ = new BehaviorSubject<Map<string, any>>(
      new Map<string, any>(
        this.cookieService.check(urlParamsCookieName) ? 
          this.getParamsFromCookie().map((value, i) => [this._lookupUrlSegments[i], value]) : 
          this._lookupUrlSegments.map(segment => [segment, null])
      )
    );
    this._paramsMap$.subscribe(_ => {
      this.updateCookie();    //*********** PROBLEM HERE ***********
    });
    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      map(event => {
        this.updateCookie();    //*********** PROBLEM HERE ***********
      })
    ).subscribe();
    this.updateCookie();    //*********** PROBLEM HERE ***********
  }

  addParam(key: string, value: any) {
    this._paramsMap$.next(this.paramsMap.set(key, value));
  }

  public getParams(...lookupKeyword: string[]): Map<string, any> {
    if (lookupKeyword) {
      return new Map<string, any>([...this.paramsMap.keys()]
        .filter(key => lookupKeyword.includes(key))
        .map(key => [key, this.paramsMap.get(key)]));
    }
    return this.paramsMap;
  }

  private getParamsFromCookie(): any[] {
    return Array.from(this.cookieService.get(urlParamsCookieName).split(','));
  }

  private updateCookie() {
    if (this.cookieService.check(urlParamsCookieName)) {
      this.cookieService.delete(urlParamsCookieName, '/');
    }
    this.setCookie();
  }

  private setCookie() {
    this.cookieService.set(urlParamsCookieName, [...this.paramsMap.values()].toLocaleString(), undefined, '/');
  }

}

【问题讨论】:

  • 嘿,您找到解决方案了吗?我面临同样的问题。
  • @Laxminarayan 不,我最终选择不为此目的使用 cookie
  • 好的。感谢您的快速回复
  • @dc_Bita98 - 您是否还在设置 cookie 的任何地方都将路径指定为“/”?这仍然导致问题吗?我遇到了类似的问题。有人对此有解决方案吗?这真的很烦人,正如你 OP 提到的,并不是一直都在发生

标签: angular cookies setcookie angular-cookies ngx-cookie-service


【解决方案1】:
import { CookieService } from 'ngx-cookie-service';
...
let Future = toInteger(Date.now()) + 5 * 60000;
this.cookies.set('session', data, { 
   expires: Future, 
   path: '/', 
   sameSite: 'Strict' 
});

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多