All the data present in my store is persisted in the localStorage but I would like to persist only my user, the user is stored in a nuxt.js store.
Here is my store/login.js
export const state = () => ({
user: ''
})
export const mutations = {
setUser(state, newUser) {
state.user = newUser;
}
}
export const getters = {
getUser(state) {
return state.user
}
}
And I am using vuex-persist plugin to persist the data.
Here is my plugin/vuex-persist :
import VuexPersistence from 'vuex-persist';
export default ({ store }) => {
new VuexPersistence({
key: 'vuex',
storage: window.localStorage,
}).plugin(store);
}
Thanks for your help !
From the vuex-persist documentation:
const vuexCookie = new VuexPersistence<State, Payload>({
restoreState: (key, storage) => Cookies.getJSON(key),
saveState: (key, state, storage) =>
Cookies.set(key, state, {
expires: 3
}),
modules: ['user'], //only save user module
filter: (mutation) => mutation.type == 'logIn' || mutation.type == 'logOut'
})
The options object has a "modules" key that expects a string[] value, with the names of the modules you DO want to serialize.
Constructor param: modules
Value type: string[]
Description: List of modules you want to persist. (Do not write your own reducer if you want to use this)