【问题标题】:Can Angular's HttpRequest objects be saved in local storage?Angular Http Request 对象可以保存在本地存储中吗?
【发布时间】:2019-05-11 09:16:26
【问题描述】:

在我的 Angular/Ionic 应用程序中,我想将 HttpRequest 对象保存到本地存储(使用 Ionic 的本机存储),在某些特定条件下从 http 拦截器调用:

public addRequestToQueue(request) {
    let queuedRequests = [];
    this.storage.get('queuedRequests').then((requests) => {
      if(requests) {
        queuedRequests = requests;
      }
      queuedRequests.push(request);
      this.storage.set('queuedRequests', queuedRequests);
    });
  }

但我在控制台中遇到错误:

    Uncaught (in promise): DataCloneError: Failed to execute 'put' on 'IDBObjectStore': function () {
                _this.headers = new Map();
                Object.keys(headers).forEach(function ...<omitted>... } could not be cloned.
Error: Failed to execute 'put' on 'IDBObjectStore': function () {
                _this.headers = new Map();
                Object.keys(headers).forEach(function ...<omitted>... } could not be cloned.

如何将它们保存在本地存储中以备后用?可能吗?也许应该使用某种序列化方法?

【问题讨论】:

  • 我必须先问我是否需要这样做,因为我可以在需要时提出请求。
  • @Antoniodelamata 在设备离线的情况下,我们想修改请求并稍后自动重新发送。
  • service worker 会更适合这个任务。看看工作箱 (developers.google.com/web/tools/workbox)

标签: angular typescript ionic-framework angular-http


【解决方案1】:

更新 在研究了一下这个错误后,我发现通常有一个框架或工具向 Array 原型添加属性,这使得它无法复制keys。您可以在 pouchdb 上引用以下github discussion,用户在其中遇到了类似的错误。

只是为了验证没有其他东西干扰您的存储,请尝试对请求进行编码并将其放入:

public addRequestToQueue(request) {
  this.storage.set('request', JSON.stringify(request));
});

这个

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2011-08-21
相关资源
最近更新 更多