问题描述
我正在寻找一种在 VueJS 中缓存 http 响应数据的最佳方法,现在我将 Vuex Store 用于我的博客.我想在请求到服务器时缓存所有响应数据.
I'm finding a best way to Cache http response data in VueJS, Now I use Vuex Store to my Blog. I want to cache all response data when it requested into server.
具体来说,这是我的博客:
Specifically, this is my blog:
当我使用1、3、4通过路由器向博客详细信息请求数据时,我有响应数据.我如何缓存它,然后我在同一会话中重新路由到 1、3、4,它不会重新获取数据并获取数据缓存以显示?
When I request data by router to blog detail with 1, 3, 4, I have response data. How can I cache it and then I re-route to 1, 3, 4 in the same session, it not re-fetch data and get data cache to display?
现在我使用 Vuex,如果必须存储太多数据,我认为它很慢.
Now I use Vuex and I think it slow if has to store too many data.
推荐答案
Service Workers 就是为此而构建的.他们可以拦截和缓存您页面上的所有请求.只需做一些额外的工作,您就可以轻松添加离线功能.
Service Workers were built for this. They can intercept and cache all requests made on your page. With a little extra work, you can easily add offline capabilities.
您还可以使用 缓存 API 和 window.caches
,请注意浏览器支持.
You can also use the Cache API with window.caches
, mind the browser support though.
另一种方法是使用 LocalStorage/IndexedDB 手动存储您的响应,但这需要更多工作.
Another way is to use LocalStorage/IndexedDB to manually store your responses, but that's more work.
这篇关于VueJS:缓存 http 响应数据的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!