cool-team-official/cool-uni

pinia如何使用持久化插件?

panghujiajia opened this issue · 1 comments

尝试自己主动使用无效

// plugin.js
import { createPersistedState } from 'pinia-plugin-persistedstate';

export const storePlugin = createPersistedState({
    storage: {
        getItem(key) {
            return uni.getStorageSync(key);
        },
        setItem(key, value) {
            uni.setStorageSync(key, value);
        }
    }
});

// main.js
import { createSSRApp } from "vue";
import { bootstrap } from "/@/cool";
import "/@/mock";
import App from "./App.vue";

import { createPinia } from "pinia";
import { storePlugin } from "/@/store/storePlugin";

const pinia = createPinia();
pinia.use(storePlugin);

export function createApp() {
	const app = createSSRApp(App);

	app.use(pinia);

	// 启动
	bootstrap(app);

	return {
		app,
	};
}

问题解决了

在bootstrap.ts中使用即可

import { createPinia } from "pinia";
import { App } from "vue";
import mitt from "mitt";
import { useEps } from "./core/eps";

import { storePlugin } from "/@/store/storePlugin";

export function bootstrap(app: App) {
	// 通信
	app.provide("mitt", mitt());

	// 缓存
	app.use(createPinia().use(storePlugin));

	// eps
	useEps();
}