1. 程式人生 > >解決vue頁面刷新後原先獲取的vuex中state消失的問題

解決vue頁面刷新後原先獲取的vuex中state消失的問題

聲明 listener item json repl tps event ace 狀態

在 app.vue中的created函數中寫如下代碼:localstorage和sessionStorage都可以

//在頁面加載時讀取sessionStorage裏的狀態信息

if (sessionStorage.getItem("store") ) {

this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(sessionStorage.getItem("store"))))

}

//在頁面刷新時將vuex裏的信息保存到sessionStorage裏

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

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

})
---------------------
作者:goodaxuan
來源:CSDN
原文:https://blog.csdn.net/goodaxuan/article/details/82113123
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

解決vue頁面刷新後原先獲取的vuex中state消失的問題