pinia-persist
Version:
Persist and rehydrate your Pinia state between page reloads
63 lines (44 loc) • 1.43 kB
Markdown
> This package works for Vue 2 & 3 by the power of [Vue Demi](https://github.com/vueuse/vue-demi)!
```shell
$ npm i pinia-persist
```
```html
<script src="https://unpkg.com/pinia-persist"></script>
```
```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 }))
```
Please read [CONTRIBUTING.md](/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull
requests to us.
This project use [SemVer](https://semver.org/) for versioning. For the versions available, see the tags on this repository.
This project is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details