Home > Mobile >  How to persist Vue 3 store without Vuex?
How to persist Vue 3 store without Vuex?

Time:03-05

I'm using the vue 3 store to pass around values between components, in this case an image link.

store.js:

import {reactive} from "vue";

const re = reactive({})

export default {
    re
}

An image link is assigned in a component:

store.re.article = '../../images/1.jpg'

and recalled in another component:

<img :src="store.re.article">

Problem now is that the store is not persistent. When I refresh the page, the image does not get loaded because the link does not exist anymore. I could use localstorage/sessionstorage but was wondering if there is a more elegant way of achieving persistant store (like in vuex for example).

CodePudding user response:

This tool existed, but it's not maintained anymore. https://github.com/robinvdvleuten/vuex-persistedstate

I think you'll be fine using Session or Localstorage.

CodePudding user response:

for those who use pinia there is a very recent solution being worked on:

ttps://github.com/prazdevs/pinia-plugin-persistedstate

  • Related