AppCraft的Vuex 模块已经配置好,可以在组件中调用 Vuex 的 actions 来更新和清除 userInfo。以下是如何在组件中集成这个 store 模块的方法:
1. 更新组件中的 clearStorageSync() 方法
在组件中调用 actions,确保在清除缓存时同步更新全局状态。
methods: {
clearStorageSync() {
const keys = ['settingsBasic', 'extension', 'token']; // userInfo 将通过 Vuex 清除
let errors = [];
keys.forEach(key => {
uni.removeStorage({
key: key,
fail: () => {
errors.push(key);
}
});
});
if (errors.length === 0) {
// 调用 Vuex action 清除 userInfo
this.$store.dispatch('user/clearUserInfo').then(() => {
uni.reLaunch({
url: '/pages/my/my' // 重新加载页面
});
});
} else {
uni.showToast({
title: `清除失败的键: ${errors.join(', ')}`,
icon: 'none'
});
}
}
}
2. 组件中使用 userInfo 作为响应式数据
从 Vuex 获取 userInfo,以便在组件中响应 userInfo 的变化:
computed: {
userInfo() {
return this.$store.state.user.userInfo;
}
}
3. 使用 updateUserInfo 更新 userInfo
在需要更新 userInfo 的地方调用 updateUserInfo:
this.$store.dispatch('user/updateUserInfo', newUserInfo);



发表回复
要发表评论,您必须先登录。