pinia-persistent-store-plugin
v1.0.3
Published
pinia的持久化存储插件,支持localStorage、sessionStorage、indexedDB
Downloads
2
Readme
pinia-persistent-store-plugin
介绍
pinia的持久化存储插件,支持localStorage、sessionStorage、indexedDB,目前只在公司内部测试使用。
安装教程
npm install pinia-persistent-store-plugin -S
使用说明
//main.ts
import createStoragePlugin from "pinia-persistent-store-plugin"
import { createPinia } from "pinia";
const pinia = createPinia();
pinia.use(
createStoragePlugin({
prefix: "prefix_",//本地存储时添加前缀
})
);
//counter.ts
const useCounterStore = defineStore("counter", {
persist: {
save_key: "user_user",
type: "DB",
paths: ["count"],
},
state: () => {
return { count: 0,list:[] };
},
actions: {},
});
const useCounterStore = defineStore("counter", {
persist:true,
state: () => {
return { count: 0,list:[] };
},
actions: {},
});
persist参数
{
save_key: "",//本地存储的key,createStoragePlugin传入前缀会拼接前缀
type: "L",//本地存储的类型默认是L(localStorage),还有S(sessionStorage)、DB(indexedDB)
paths: [],//state重某些需要本地存储的字段
debounce: {//本地存储的防抖,频繁修改state会频繁触发本地存储,可以开启防抖,在某些时间内频发修改state不会触发本地存储
open: false, //是否开启防抖
wait: 1000,//防抖时间
},
};