UNPKG

pinia-persist

Version:

Persist and rehydrate your Pinia state between page reloads

63 lines (44 loc) 1.43 kB
# pinia-persist > This package works for Vue 2 & 3 by the power of [Vue Demi](https://github.com/vueuse/vue-demi)! ## Getting Started ### Installation #### NPM ```shell $ npm i pinia-persist # yarn add pinia-persist ``` #### CDN ```html <script src="https://unpkg.com/pinia-persist"></script> ``` ### Usage ```ts import { ref } from 'vue' // import { ref } from '@vue/composition-api' import { createPinia, defineStore } from 'pinia' import { createPersistPlugin } from 'pinia-persist' const pinia = createPinia() pinia.use(createPersistPlugin()) const useCounterStore = defineStore( 'counter-store', () => { return { count: ref(0), } }, { persist: { storage: window.localStorage } }, ) // const counterStore = defineStore('counter-store', { // state() { // return { count: 0 } // }, // persist: { storage: window.localStorage }, // }) const counterStore = useCounterStore() counterStore.count++ // fires window.localStorage.setItem('counter-store', JSON.stringify({ count: 0 })) ``` ## Contributing Please read [CONTRIBUTING.md](/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us. ## Versioning This project use [SemVer](https://semver.org/) for versioning. For the versions available, see the tags on this repository. ## License This project is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details