如果用户选择了“记住我”复选框,我想要从sessionStorage切换到localStorage,同时我使用vuex-persistedstate
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;
});
关键是我想要像这样进行更改
state: {
flag: false
},
plugins: [
createPersistedState({
storage: flag ? window.localStorage : window.sessionStorage,
}),
],
我希望flag根据用户在登录时选择的“记住我”复选框而改变,所以当用户选择复选框时,flag变为true,并且所有数据保存在localStorage中
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
根据@Estus Flask的评论,我使用了自定义存储,并通过直接调用localStorage来管理“记住我”选项,并在localStorage中设置了一个标志。
plugins: [ createPersistedState({ paths: ["authentication.userAuthenticationInfo"], storage: { getItem(key) { if (localStorage.getItem("rememberMe") == 1) { return localStorage.getItem(key); } else { return Cookies.get(key); //return sessionStorage.getItem(key); } }, setItem(key, value) { if (localStorage.getItem("rememberMe") == 1) { localStorage.setItem(key, value); } else { Cookies.set(key, value); //sessionStorage.setItem(key, value); } }, removeItem(key) { if (localStorage.getItem("rememberMe") == 1) { localStorage.removeItem(key); } else { Cookies.remove(key); //sessionStorage.removeItem(key); } }, }, }), ],并且我在每次注销或401响应时清除localStorage。