【发布时间】: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