【问题标题】:Cache ttl of cache in VueVue中缓存的缓存ttl
【发布时间】:2018-10-08 07:15:09
【问题描述】:

我在我的项目中使用 Vuex 和 vuex-persistedstate。
如何更改 Vue 中本地存储的生存时间(ttl)?
对不起,我的英语不好 附言我使用这个解决方案When do items in HTML5 local storage expire?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 现在,对于这个问题,我有 2 种不同的解决方案。一用店里的js-cookie,二用解决方案stackoverflow.com/questions/2326943/…。现在,我在我的项目中使用 window.localStorage

标签: vue.js vuex


【解决方案1】:

vuex-persistedstate 似乎正在使用这个 cookie 库:

https://github.com/js-cookie/js-cookie#expires

这个例子也有过期的属性(取自他们的文档):

const store = new Store({
  // ...
  plugins: [
    createPersistedState({
      storage: {
        getItem: key => Cookies.get(key),
        setItem: (key, value) =>
          Cookies.set(key, value, { expires: 3, secure: true }),
        removeItem: key => Cookies.remove(key),
      },
    }),
  ],
})

这里的数字 3 是天数,如果你想要更少,可以按照这个常见问题解答:

https://github.com/js-cookie/js-cookie/wiki/Frequently-Asked-Questions#how-to-make-the-cookie-expire-in-less-than-a-day

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多