Nuxt.js:未定义窗口

问题描述

我正在使用Nuxt.js。设置vuex-persist出现以下问题。有人可以帮我吗?

store / index.js

image one

store / LangModule.js

image two

image three

解决方法

重要的是要知道Nuxt在服务器端和客户端都可以工作,因此您不能在所有地方都使用window,因为它仅存在于客户端。

您可以声明此插件为无SSR,以避免在服务器端运行它。

遵循documentation

nuxt.config.js

export default {
  plugins: [
    { src: '~/plugins/vuex-persist',ssr: false }
  ]
}

〜/ plugins / vuex-persist.js

import VuexPersistence from 'vuex-persist'
 
export default ({ store }) => {
  new VuexPersistence({
  /* your options */
  }).plugin(store);
}