最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nuxt.js: window is not defined - Stack Overflow

programmeradmin1浏览0评论

I am using Nuxt.js. The following problem occurred with setting up vuex-persist. Could someone help me?

store/index.js

store/LangModule.js

I am using Nuxt.js. The following problem occurred with setting up vuex-persist. Could someone help me?

store/index.js

store/LangModule.js

Share Improve this question edited Sep 29, 2020 at 19:21 10 Rep 2,2707 gold badges21 silver badges33 bronze badges asked Sep 29, 2020 at 16:35 Varga MárkVarga Márk 231 silver badge5 bronze badges 1
  • you cant call window on store file. you have to import it as plugin and call it on cliend side not server side, please read the documentation. already mention there npmjs./package/vuex-persist – Jazuly Commented Sep 29, 2020 at 23:56
Add a ment  | 

1 Answer 1

Reset to default 3

It's important to know Nuxt works on both server and client sides, so you cannot use window in all places since it only exists on client side.

You can declare this plugin SSR-free to avoid running it on server side.

Following the 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);
}
发布评论

评论列表(0)

  1. 暂无评论