feat: 添加pinia状态管理库和Tabbar组件
- 添加pinia状态管理库和Tabbar组件 - 引入axios和fant-axios-adapter - 在main.ts中使用pinia和persist插件 - 在App.vue中修改标题 - 在useUser.ts中添加logout方法 - 添加persist.ts文件 - 修改page-wrapper.vue和TabBar.vue中的代码 - 修改index.vue和login.vue中的代码
This commit is contained in:
17
src/stores/persist.ts
Normal file
17
src/stores/persist.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import type { PiniaPluginContext } from "pinia";
|
||||
import { deepClone } from "wot-design-uni/components/common/util";
|
||||
|
||||
export function persist({ store }: PiniaPluginContext) {
|
||||
// 暂存State
|
||||
let persistState = deepClone(store.$state);
|
||||
// 从缓存中读取
|
||||
const storageState = uni.getStorageSync(store.$id);
|
||||
if (storageState) {
|
||||
persistState = storageState;
|
||||
}
|
||||
store.$state = persistState;
|
||||
store.$subscribe(() => {
|
||||
// 在存储变化的时候将store缓存
|
||||
uni.setStorageSync(store.$id, deepClone(store.$state));
|
||||
});
|
||||
}
|
@ -3,9 +3,17 @@ import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
|
||||
export const useUser = defineStore("user", () => {
|
||||
const token = ref<string | null>(null);
|
||||
const userinfo = ref<User | null>(null);
|
||||
|
||||
function logout() {
|
||||
token.value = null
|
||||
userinfo.value = null
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
userinfo,
|
||||
logout,
|
||||
};
|
||||
});
|
||||
|
Reference in New Issue
Block a user