为项目添加状态管理
This commit is contained in:
36
src/store.js
Normal file
36
src/store.js
Normal 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"], // 只持久化用户信息
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user