"vuex-persistedstate to switch between sessionStorage and localStorage"
P粉295728625
2023-08-25 23:06:52
<p>If the user selects the "Remember me" checkbox, I want to switch from sessionStorage to localStorage, while I use vuex-persistedstate</p>
<pre class="brush:php;toolbar:false;">export default store(function (/* { ssrContext } */) {
const Store = createStore({
state: {
},
actions: {
setLodingMode({ commit }, newMode) {
commit("SET_LOADING_MODE", newMode);
},
resetStates({ commit }) {
commit("AUTHENTICATION_RESET_STATE");
commit("login/RESET_STATE");
},
},
modules: { login, authentication },
plugins: [createPersistedState()],
});
return Store;
});</pre>
<p>The point is I want to make changes like this</p>
<pre class="brush:php;toolbar:false;">state: {
flag: false
},
plugins: [
createPersistedState({
storage: flag ? window.localStorage : window.sessionStorage,
}),
],</pre>
<p>I want the flag to change based on the "Remember Me" checkbox the user selects when logging in, so when the user selects the checkbox, the flag becomes true and all the data is saved in localStorage</p> ;
Per @Estus Flask's comment, I used a custom storage and managed the "remember me" option by calling localStorage directly and setting a flag in localStorage.
And I clear localStorage on every logout or 401 response.