pinia-plugin-store
pinia-plugin-store
pinia tools plugin
install
npm i pinia-plugin-store
API
| property | type | description | default |
|---|---|---|---|
| stores | (string Ι StoreOptions)[] | pinia store keys(specify the store that needs to be persiste) | undefined |
| storage | storage | persistent strategy | localStorage |
| encrypt | (value: string) => string | persistent encryption | undefined |
| decrypt | (value: string) => string | persistent decryption | undefined |
Example
theme.ts
import { defineStore } from 'pinia';
export const useThemeStore = defineStore('theme_store', {
state: () => {
return {
theme: 'dark',
};
},
actions: {
setTheme(theme: string) {
this.theme = theme;
},
},
});
store.ts
simple configuration
import { createPinia } from 'pinia';
import { storePlugin } from 'pinia-plugin-store';
const store = createPinia();
const plugin = storePlugin({
stores: ['theme_store'],
});
store.use(plugin);
export default store;
specify a storage alone
import { createPinia } from 'pinia';
import { storePlugin } from 'pinia-plugin-store';
const store = createPinia();
const plugin = storePlugin({
stores: [{ name: 'theme_store', storage: sessionStorage }, 'user_store'],
storage: localStorage,
});
store.use(plugin);
export default store;
encryption
import { createPinia } from 'pinia';
import { storePlugin } from 'pinia-plugin-store';
import Utf8 from 'crypto-js/enc-utf8';
import Base64 from 'crypto-js/enc-base64';
const store = createPinia();
function encrypt(value: string): string {
return Base64.stringify(Utf8.parse(value));
}
function decrypt(value: string): string {
return Base64.parse(value).toString(Utf8);
}
const plugin = storePlugin({
stores: [{ name: 'theme_store' }],
encrypt,
decrypt,
});
store.use(plugin);
export default store;
disable encryption
import { createPinia } from 'pinia';
import { storePlugin } from 'pinia-plugin-store';
import Utf8 from 'crypto-js/enc-utf8';
import Base64 from 'crypto-js/enc-base64';
const store = createPinia();
function encrypt(value: string): string {
return Base64.stringify(Utf8.parse(value));
}
function decrypt(value: string): string {
return Base64.parse(value).toString(Utf8);
}
const plugin = storePlugin({
stores: [{ name: 'theme_store', ciphertext: false }],
storage: localStorage,
encrypt,
decrypt,
});
store.use(plugin);
export default store;
main.ts
import { createApp } from 'vue';
import store from './store';
import App from './App.vue';
const app = createApp(App);
app.use(store);
app.mount('#app');
Related Packages
pinia-use-persist
Easy to use persist plugin with configurable encryption, Currently in developmen...
2,164
pinia-plugin-persistedstate-2
Persist and rehydrate your Pinia state between page reloads
257,084
115
Version History
| Version | Released | License |
|---|---|---|
| 4.0.2 | MIT | |
| 4.0.1 | MIT | |
| 4.0.0 | MIT | |
| 3.0.9 | MIT | |
| 3.0.8 | MIT | |
| 3.0.7 | MIT | |
| 3.0.6 | MIT | |
| 3.0.5 | MIT | |
| 3.0.4 | MIT | |
| 3.0.3 | MIT | |
| 3.0.2 | MIT | |
| 3.0.1 | MIT | |
| 3.0.0 | MIT | |
| 2.3.3 | MIT | |
| 2.3.2 | MIT | |
| 2.3.1 | MIT | |
| 2.3.0 | MIT | |
| 2.2.9 | MIT | |
| 2.2.8 | MIT | |
| 2.2.7 | MIT | |
| 2.2.6 | MIT | |
| 2.2.5 | MIT | |
| 2.2.4 | MIT | |
| 2.2.3 | MIT | |
| 2.2.2 | MIT | |
| 2.2.1 | MIT | |
| 2.2.0 | MIT | |
| 2.1.37 | MIT | |
| 2.1.36 | MIT | |
| 2.1.35 | MIT |
Showing the latest 30 of 35. See every release on npm