Angular 9-路径出现ngx cookie服务故障
我正在使用ngx cookie服务包来存储一些与我的应用程序相关的数据。 我需要将这个cookie保存在基本路径Angular 9-路径出现ngx cookie服务故障,angular,cookies,angular9,setcookie,angular-cookies,Angular,Cookies,Angular9,Setcookie,Angular Cookies,我正在使用ngx cookie服务包来存储一些与我的应用程序相关的数据。 我需要将这个cookie保存在基本路径“/”,这样每次我都知道如何检索它。 我的这个cookie需要更新,当这种情况发生时,新的cookie必须存储在相同的路径中(因此在“/”)。问题是,有时当我刷新页面时,一个新的cookie保存在一个新路径中,因此当我尝试使用this.cookieService.get(cookieName,“/”)检索它时,显然失败了。尽管我明确声明使用'/'作为路径,但仍会发生这种情况。这种情况并
“/”
,这样每次我都知道如何检索它。
我的这个cookie需要更新,当这种情况发生时,新的cookie必须存储在相同的路径中(因此在“/”
)。问题是,有时当我刷新页面时,一个新的cookie保存在一个新路径中,因此当我尝试使用this.cookieService.get(cookieName,“/”)
检索它时,显然失败了。尽管我明确声明使用'/'
作为路径,但仍会发生这种情况。这种情况并不总是发生,这会导致调试更加困难
这是我使用cookies的服务
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, '/');
}
}
const-urlparamscookeName='errepiupp-url-params';
/**
*此服务跟踪用于从后端检索对象的密钥。
*/
@注射的({
providedIn:'根'
})
导出类KeyLookupService{
private _lookupUrlSegments=[“学生”、“宏”、“lto”、“sto”、“报告”];
private _paramsMap$:行为主体;
private get paramsMap(){
返回此值。_paramsMap$.getValue()
}
建造师(
私人厨师服务:厨师服务,
专用路由器
) {
如果(!this.router.url.includes('auth')&&&!this.cookieService.check(urlparamscookeName)){
this.router.navigate(['/auth']);
}
此._paramsMap$=新行为主体(
新地图(
这个.cookieService.check(urlParamScookeName)?
this.getParamsFromCookie().map((值,i)=>[this.\u lookupUrlSegments[i],value]):
此.u lookupUrlSegments.map(段=>[segment,null])
)
);
此._参数映射$.subscribe(=>{
这里的.updateCookie();//**********问题***********
});
此为.router.events.pipe(
过滤器(事件=>NavigationEnd的事件实例),
映射(事件=>{
这里的.updateCookie();//**********问题***********
})
).subscribe();
这里的.updateCookie();//**********问题***********
}
addParam(键:字符串,值:任意){
this.paramsMap$.next(this.paramsMap.set(key,value));
}
公共getParams(…lookupKeyword:string[]):映射{
if(lookupKeyword){
返回新映射([…this.paramsMap.keys()]
.filter(键=>lookupKeyword.includes(键))
.map(key=>[key,this.paramsMap.get(key)]);
}
返回此.paramsMap;
}
私有getParamsFromCookie():任意[]{
返回Array.from(this.cookieService.get(urlparamscookeName).split(',');
}
私有updateCookie(){
if(this.cookieService.check(urlparamscookeName)){
this.cookieService.delete(urlparamscookeName,“/”);
}
这个.setCookie();
}
私有setCookie(){
this.cookieService.set(urlParamScookeName,[…this.paramsMap.values()].toLocaleString(),未定义“/”);
}
}
嘿,你找到解决方案了吗?我也面临同样的问题。@Laxminarayan不,我最终选择了不使用cookies。谢谢你的快速回复response@dc_Bita98-您是否在设置该cookie的任何位置都将路径指定为“/”?这仍然是问题的根源吗?我遇到了类似的问题。有人有办法解决这个问题吗?这真的很烦人,正如你提到的,不是一直都在发生