如何使用角度服务人员访问本地存储? [英] How do I access the local storage using service workers in angular?

本文介绍了如何使用角度服务人员访问本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,现在我正在使用Angular 6 webApp,它首先处于离线状态。我能够设置一个功能齐全的服务程序,该服务程序可以检索和存储请求和响应,而不能检索和存储请求和响应。但是,在某一时刻,在注册同步事件时,我需要刷新令牌,这些令牌存储在我的webApp的本地存储中。问题是服务工作人员不允许直接访问本地存储,这意味着我无权访问驻留在本地存储中的令牌。我如何使用某些API使用服务工作人员中的本地存储?实现我想要做的事情的其他策略(通过重放失败请求的提取调用并附加一个更新的令牌来执行同步事件,以便通过适当的授权来同步数据)。我什至考虑过要使用postmessage()API进行尝试,但是它用处不大,因为我只有一个工人在不使用角度应用程序的情况下工作。

Okay now i am working on Angular 6 webApp and it is offline first. I am able to setup a fully functional service worker which can retrieve and store requests and response and what not. But, at one point , while registering a sync event, i needed refresh tokens which are stored in the local storage of my webApp. The problem is that service workers are not allowed to access local storage directly, means i don't have any access to the tokens residing in the local storage.How can i make use of local storage in service worker using some api or is there any other strategy to achieve what i want to do (to execute a sync event by replaying the fetch call of failed requests and attaching an updated token with it so that the data gets synced with proper authorization). I even thought of trying it with postmessage() API but it isn't of much use as i have just one worker which is working without any intervention with the angular application.

推荐答案

您可以使用IndexedDB在浏览器DB中存储令牌。它的工作方式类似于本地存储。

You can use IndexedDB for storing the token in browser DB. It works similar to local-storage.

https:/ /github.com/mozilla/localForage 如果您想为IndexedDB提供更简单的界面。

https://github.com/mozilla/localForage if you want a simpler interface for IndexedDB.

您可以使用importScripts在服务工作者中导入localForage库,请参见例如: https://github.com/marco -c / mercurius / blob / master / static / sw-push.js

You can import the localForage lib in a service worker using importScripts, see for example: https://github.com/marco-c/mercurius/blob/master/static/sw-push.js

这篇关于如何使用角度服务人员访问本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆