Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window is not defined using vuex-persist and NUXT #88

Open
warheartvik opened this issue Dec 14, 2018 · 8 comments
Open

window is not defined using vuex-persist and NUXT #88

warheartvik opened this issue Dec 14, 2018 · 8 comments

Comments

@warheartvik
Copy link

warheartvik commented Dec 14, 2018

Hello,

I followed the instructions on https://www.npmjs.com/package/vuex-persist. But I got 'window is not defined' error. I'm using nuxt and typescript.

Nuxt Config:
{ src: '~/plugins/vuex-persist.ts', ssr: false },

~/plugins/vuex-persist.ts:
import VuexPersistence from 'vuex-persist'

export default ({store}) => {
if (process.client){
return new VuexPersistence({
storage: window.localStorage
}).plugin(store);
}

}
~/store/index.ts
import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist';
Vue.use(Vuex);

const vuexLocal = new VuexPersistence({
storage: window.localStorage
})

export default () => new Vuex.Store({
state: {
authUser: null,
token: process.client ? localStorage.getItem('token') : null
},

mutations: {
set_user: function (state, user) {
//@ts-ignore
state.authUser = user;
},
set_token (state, token){
//@ts-ignore
state.token = token;
}
},

actions: {
async iniciarSesion ({ commit }, datosUsuario) {

  commit('set_user', datosUsuario.usuario);

  if (process.client){
    localStorage.setItem('token', datosUsuario.token);
  }
  
  commit('set_token', datosUsuario.token);
}, //iniciar sesion final del metodo

},
getters: {
user (state) {
//@ts-ignore
return state.authUser;

}

},

plugins: [vuexLocal.plugin]
});

@kpturner
Copy link

Bit difficult to follow ^^ because you haven't formatted the code as
this is code above, but anyway I was looking to use this plug-in with Nuxt (typescript) also but I am a bit reticent until you get an answer. Obviously window.anything isn't going to fly when executed server side. It does beg the question for me as somewhat of a n00b as to where the store exists on the server-side anyway. Clearly when server-side rendering is taking place the vuex store can be accessed and populated but I have no clue how it is synced between the client and the server.

@championswimmer
Copy link
Owner

championswimmer commented Dec 18, 2018 via email

@kpturner
Copy link

Isn't the OP using it is a client-only plugin then? The README says to use ssr: false and he is - but still gets the window is not defined error.

@kpturner
Copy link

OK I see what he did wrong. I have it at least storing state in localStorage now. I have another issue that I will log separately.

@kyriediculous
Copy link

Note that no-ssr still "renders" the content internally (but doesn't render the html, not until the parent is mounted).

@nasermirzaei89
Copy link

I used this code:

function getPlugins() {
    let plugins = []

    if (process.browser) {
        const vuexLocal = new VuexPersistence<RootState>({
            storage: window.localStorage
        })

        plugins.push(vuexLocal.plugin)
    }
}

export const plugins = getPlugins()

So, I get rid of error window is not defined

@topzdev
Copy link

topzdev commented Apr 13, 2020

@nasermirzaei89 you forgot to return the plugins

@nasermirzaei89
Copy link

@topzdev as I see in nuxt.js guide, we should only export it as plugins:

https://nuxtjs.org/guide/vuex-store/#plugins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants