問題描述:在設置好store之后,刷新頁面后導致store中的內容消失。
解決辦法:在刷新前將store中的內容添加到sessionStorage。
實際代碼:在App.vue的created()中存儲與獲取
// 在頁面加載時讀取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)) })
截圖說明: