pinia持久化存储
This commit is contained in:
parent
18d6627f41
commit
58da6a52b9
12
main.js
12
main.js
@ -5,13 +5,14 @@ import store from './store' // Vuex store(如果还在用)
|
||||
import { install } from './plugins' // 自定义插件
|
||||
import './permission' // 路由权限
|
||||
import { useDict } from '@/utils/dict'
|
||||
import { createPinia } from 'pinia'
|
||||
// import { createPinia } from 'pinia'
|
||||
import * as Pinia from 'pinia';
|
||||
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
|
||||
|
||||
export function createApp() {
|
||||
const app = createSSRApp(App)
|
||||
const pinia = createPinia()
|
||||
|
||||
// const pinia = createPinia()
|
||||
const pinia = Pinia.createPinia()
|
||||
// ✅ 安全注册持久化插件
|
||||
if (piniaPluginPersistedstate) {
|
||||
pinia.use(piniaPluginPersistedstate)
|
||||
@ -20,7 +21,7 @@ export function createApp() {
|
||||
}
|
||||
|
||||
app.use(pinia)
|
||||
app.use(store) // 如果你还在用 Vuex,否则可删除
|
||||
// app.use(store) // 如果你还在用 Vuex,否则可删除
|
||||
|
||||
// 挂载全局方法
|
||||
app.config.globalProperties.useDict = useDict
|
||||
@ -29,6 +30,7 @@ export function createApp() {
|
||||
install(app)
|
||||
|
||||
return {
|
||||
app
|
||||
app,
|
||||
Pinia
|
||||
}
|
||||
}
|
||||
@ -24,22 +24,23 @@ export const useOrderManagerStore = defineStore('orderManager', () => {
|
||||
clearOrders
|
||||
};
|
||||
}, {
|
||||
persist: {
|
||||
storage: {
|
||||
// 读取:从 uni.storage 获取
|
||||
getItem(key) {
|
||||
const value = uni.getStorageSync(key);
|
||||
// 如果有值,解析 JSON;否则返回 null(Pinia 会处理默认值)
|
||||
return value ? JSON.parse(value) : null;
|
||||
},
|
||||
// 写入:保存到 uni.storage
|
||||
setItem(key, value) {
|
||||
uni.setStorageSync(key, JSON.stringify(value));
|
||||
},
|
||||
// 可选:删除(一般不需要手动调用)
|
||||
removeItem(key) {
|
||||
uni.removeStorageSync(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
// persist: {
|
||||
// storage: {
|
||||
// // 读取:从 uni.storage 获取
|
||||
// getItem(key) {
|
||||
// const value = uni.getStorageSync(key);
|
||||
// // 如果有值,解析 JSON;否则返回 null(Pinia 会处理默认值)
|
||||
// return value ? JSON.parse(value) : null;
|
||||
// },
|
||||
// // 写入:保存到 uni.storage
|
||||
// setItem(key, value) {
|
||||
// uni.setStorageSync(key, JSON.stringify(value));
|
||||
// },
|
||||
// // 可选:删除(一般不需要手动调用)
|
||||
// removeItem(key) {
|
||||
// uni.removeStorageSync(key);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
persist: true
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user