为项目添加状态管理

This commit is contained in:
kron
2025-05-25 23:51:10 +08:00
parent 222a1fd7aa
commit 6931646737
5 changed files with 90 additions and 21 deletions

36
src/store.js Normal file
View File

@@ -0,0 +1,36 @@
import { defineStore } from "pinia";
// 定义游戏相关的 store
export default defineStore("store", {
// 状态
state: () => ({
user: {
username: "游客",
},
}),
// 计算属性
getters: {
getUsername: (state) => {
return state.user.username;
},
},
// 方法
actions: {
updateUsername(newUsername) {
this.user.username = newUsername;
},
},
// 开启数据持久化
persist: {
enabled: true,
strategies: [
{
storage: uni.getStorageSync,
paths: ["user"], // 只持久化用户信息
},
],
},
});