我有以下问题,我试图从我的浏览器中获取cookie,但是我需要在它们出现时获得它们。它们中的一些不会立即出现,所以我试图使用下面的代码来获取它们。我在编辑中看到的错误是:
类型“{}”上不存在属性“订阅”
import { CookieService } from 'ngx-cookie-service';
this._cookieService.getAll().subscribe((result) => {
console.log(result);
});有人能帮忙吗?
发布于 2019-10-16 08:54:07
ngx-cookie-service的getAll()方法返回不可观测的对象。
它是:getAll():{}
只需使用:
result = this._cookieService.getAll();检查文档
发布于 2019-10-16 08:53:55
与文档对应,getAll函数返回带有键值对的映射。所以没必要订阅它。
https://www.npmjs.com/package/ngx-cookie-service
var result = this._cookieService.getAll();如果您想得到cookie更改的通知,您可能会编写一个函数,定期检查所建议的更改,比如这里的其他答案或其他问题:客户端javascript中的cookie更改是否可以通知我?。
ngx服务没有提供任何帮助来达到这个目的(我通过一些研究发现的其他cookie助手库也没有)。
发布于 2019-10-16 09:00:42
您可以使用基于rxjs timer()的轮询函数编写一个服务,该服务持续检查cookie:
private timer$: Subscription = new Subscription();
public polling(frequency = 1000 * 60 * 5) {
this.timer$.unsubscribe();
this.timer$ = timer(0, frequency).subscribe(
() => this.result = this._cookieService.getAll();
);
} 我希望这个方法对你有帮助!
https://stackoverflow.com/questions/58409266
复制相似问题