vuex page refresh data disappears

js code is run to run code in memory when all variables functions are also stored in memory

Memory refresh the page before the application is released reload script code variable recopied so you want to store these data must be stored outside,

For example: LocalStorage, Session Storage, Index DB like. These methods can allow data storage to make persistent storage on the hard disk.

Solution:

 Stored in localStorage:

 By listening refresh the page, that is, before beforeunload into the local localStorage, then read information from local localStorage when the page loads

the Created () { (beforeunload is called when the page is refreshed or closed)

  // when the page will refresh save vuex inside information to localStorage in

  window.addEventListener("beforeunload",()=>{

     localStorage.setItem("messageStore",JSON.stringify(this.$store.state))

     localStorage.getItem("messageStore") && this.$store.replaceState(Object.assign(this.$store.state,JSON.parse(localStorage.getItem("messageStore"))))

 })

}

 

https://segmentfault.com/a/1190000018419414?utm_source=tag-newest

 

Guess you like

Origin www.cnblogs.com/wangXinYuFarly/p/11823528.html