current position:Home>Solve the problem that vuex data is reset during page refresh

Solve the problem that vuex data is reset during page refresh

2022-04-29 18:12:43inticaler

vuex Data is stored in memory , Every refresh of the page will result in vuex Data in reset , The current scheme is to save the data to before page refresh localStorage

stay App.vue Internal operation :

export default {
  name: 'App',
  data() {
    return {    
    }
  },
  created() {
    // solve vuex The problem of data being reset during page refresh  

    // Read... When the page is loaded sessionStorage Status information in 
    if (sessionStorage.getItem("store")) {
      this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem("store"))))
    }

    // When the page is refreshed, it will vuex Keep the information in sessionStorage in 
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("store", JSON.stringify(this.$store.state))
    })
  },
  methods: {
  }
}

copyright notice
author[inticaler],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291637245380.html

Random recommended