feat: update
This commit is contained in:
@@ -7,23 +7,34 @@ import { handleLocalforage } from "@/utils/localforage";
|
||||
export class useUserStore {
|
||||
token: string = "";
|
||||
userId: string = "";
|
||||
name: string = "";
|
||||
avatar: string = "";
|
||||
nickname: string = "";
|
||||
|
||||
constructor() {
|
||||
makeObservable(this, {
|
||||
token: observable,
|
||||
userId: observable,
|
||||
name: observable,
|
||||
avatar: observable,
|
||||
setToken: action,
|
||||
setUserId: action,
|
||||
getToken: action,
|
||||
getUserId: action,
|
||||
isHydrated: action,
|
||||
setName: action,
|
||||
getName: action,
|
||||
setAvatar: action,
|
||||
getAvatar: action,
|
||||
setNickName: action,
|
||||
getNickName: action,
|
||||
});
|
||||
makePersistable(
|
||||
this,
|
||||
{
|
||||
// 在构造函数内使用 makePersistable
|
||||
name: "userInfo", // 保存的name,用于在storage中的名称标识,只要不和storage中其他名称重复就可以
|
||||
properties: ["token", "userId"], // 要保存的字段,这些字段会被保存在name对应的storage中,注意:不写在这里面的字段将不会被保存,刷新页面也将丢失:get字段例外。get数据会在数据返回后再自动计算
|
||||
properties: ["token", "userId", "name", "avatar", "nickname"], // 要保存的字段,这些字段会被保存在name对应的storage中,注意:不写在这里面的字段将不会被保存,刷新页面也将丢失:get字段例外。get数据会在数据返回后再自动计算
|
||||
storage: handleLocalforage, // 保存的位置:可以是localStorage,sessionstorage
|
||||
// removeOnExpiration: true, //如果 expireIn 具有值且已过期,则在调用 getItem 时将自动删除存储中的数据。默认值为 true。
|
||||
// stringify: false, //如果为 true,则数据在传递给 setItem 之前将是 JSON.stringify。默认值为 true。
|
||||
@@ -61,4 +72,22 @@ export class useUserStore {
|
||||
setUserId(userId: string) {
|
||||
this.userId = userId;
|
||||
}
|
||||
setName(name: string) {
|
||||
this.name = name;
|
||||
}
|
||||
setNickName(name: string) {
|
||||
this.nickname = name;
|
||||
}
|
||||
getNickName() {
|
||||
return this.nickname ? this.nickname : null;
|
||||
}
|
||||
setAvatar(avatar: string) {
|
||||
this.avatar = avatar;
|
||||
}
|
||||
getName() {
|
||||
return this.name ? this.name : null;
|
||||
}
|
||||
getAvatar() {
|
||||
return this.avatar ? this.avatar : null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user