0.0.2 • Published 4 years ago
pinia-persistedstore v0.0.2
pinia-persistedstore
Pinia 全局数据界面刷新缓存
Install
npm install --save pinia-persistedstore
or
yarn add pinia-persistedstore -S
The UMD build is also available on unpkg:
<script src="https://unpkg.com/pinia-persistedstore/dist/pinia-persistedstore.umd.js"></script>
You can find the library on window.createPiniaPersistedStore
.
Usage
import { createPinia } from "pinia";
import createPiniaPersistedStore from "pinia-persistedstore";
const store = createPinia();
store.use(createPiniaPersistedStore())
Examples
// @/storePinia/index.ts
import { createPinia } from 'pinia'
import createPiniaPersistedStore from 'pinia-persistedstore'
import SecureLS from 'secure-ls'
// 自定义 secure-ls metaKey
class CustomSecureLS extends SecureLS {
[x: string]: any
init() {
this.utils.metaKey = 'xxx'
super.init()
}
}
const ls = new CustomSecureLS({ isCompression: false, encodingType: 'aes' })
const store = createPinia()
store.use(
createPiniaPersistedStore({
key: 'client',
// 需要存储的数据
paths: ['count'],
// 自定义storage
storage: {
getItem: (key) => ls.get(key),
setItem: (key, value) => ls.set(key, value),
removeItem: (key) => ls.remove(key)
}
})
)
export default store
// @/storePinia/moudles/productStore.ts
import { defineStore } from 'pinia'
export const productStore = defineStore({
id: 'productStore',
state: () => {
return {
count: 0,
name: ''
}
},
actions: {
increment() {
this.count++
}
}
})
// demo.ts
import { productStore } from '@/storePinia/moudles/productStore'
export default defineComponent({
name: 'Demo',
setup: () => {
const pudcStore = productStore()
// pudcStore.count
// pudcStore.increment()
}
...
})
API
createPersistedState([options])
Creates a new instance of the plugin with the given options. The following options can be provided to configure the plugin for your specific needs:
key <String>
: The key to store the persisted state under. Defaults topinia
.paths <Array>
: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. If an empty array is given, no state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" Defaults toundefined
.reducer <Function>
: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values.subscriber <Function>
: A function called to setup mutation subscription. Defaults tostore => handler => store.subscribe(handler)
.storage <Object>
: Instead of (or in combination with)getState
andsetState
. Defaults to localStorage.getState <Function>
: A function that will be called to rehydrate a previously persisted state. Defaults to usingstorage
.setState <Function>
: A function that will be called to persist the given state. Defaults to usingstorage
.filter <Function>
: A function that will be called to filter any mutations which will triggersetState
on storage eventually. Defaults to() => true
.overwrite <Boolean>
: When rehydrating, whether to overwrite the existing state with the output fromgetState
directly, instead of merging the two objects withdeepmerge
. Defaults tofalse
.arrayMerger <Function>
: A function for merging arrays when rehydrating state. Defaults tofunction (store, saved) { return saved }
(saved state replaces supplied state).rehydrated <Function>
: A function that will be called when the rehydration is finished. Useful when you are using Nuxt.js, which the rehydration of the persisted state happens asynchronously. Defaults tostore => {}
fetchBeforeUse <Boolean>
: A boolean indicating if the state should be fetched from storage before the plugin is used. Defaults tofalse
.assertStorage <Function>
: An overridable function to ensure storage is available, fired on plugins's initialization. Default one is performing a Write-Delete operation on the given Storage instance. Note, default behaviour could throw an error (likeDOMException: QuotaExceededError
).
License
The MIT License (MIT). Please see License File for more information.