【问题标题】:How to implement vuex-persistedstate in vue/ssr如何在 vue/ssr 中实现 vuex-persistedstate
【发布时间】:2018-07-05 22:27:29
【问题描述】:

我需要在 vue ssr 应用中保持状态,但不明白如何实现。

作为存储,我想使用 cookie。

当我按照readme 中的说明安装插件时,什么也没发生,这很奇怪:我预计会出现错误,因为 js-cookies 调用“窗口”。

问题是:如何在vue/ssr中实现vuex-persistedstate?

我可以访问 req.cookies 中的 cookie,但无法在浏览器中设置 cookie,这是意料之中的,因为我的存储在服务器端填充,而 js-cookies 在服务器端调用。

【问题讨论】:

    标签: cookies vue.js vuex vue-server-renderer


    【解决方案1】:

    直到他们在源代码中解决了这个问题,我都是这样管理的:

    storage.js

    import CreatePersistedState from 'vuex-persistedstate'
    
    let PersistedState
    
    if (process.browser) {
      PersistedState = CreatePersistedState(yourOptions)
    }
    
    export default PersistedState
    

    store.js

    import CreatePersistedState from 'src/util/plugins/storage'
    ...
    
    const plugins = []
    
    if (process.browser) {
      plugins.push(CreatePersistedState)
    }
    
    export default function createStore() {
      return new Vuex.Store({
        state,
        modules,
        actions,
        mutations,
        getters,
        strict: false,
        plugins: plugins
      })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 2020-10-10
      • 1970-01-01
      • 2021-11-17
      • 2020-04-17
      • 2019-08-01
      • 1970-01-01
      相关资源
      最近更新 更多