📜  如何在 nuxt.js 中保持状态 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:32.447000             🧑  作者: Mango

代码示例1
You can choose any one of the below library

1 .vuex-persistedstate

2 .vuex-persist

vuex-persistedstate usage

https://www.npmjs.com/package/vuex-persistedstate

plugins/persistedstate.js

import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
import cookie from 'cookie'

export default ({store, req, isDev}) => {
  createPersistedState({
      key: 'your_key',
      paths: ['state1', 'state2',...so_on],
      storage: {
        getItem: (key) => process.client ? Cookies.getJSON(key) : cookie.parse(req.headers.cookie||'')[key],
        setItem: (key, value) => Cookies.set(key, value, { expires: 365, secure: !isDev }),
        removeItem: (key) => Cookies.remove(key)
      }
  })(store)
}