在根实例加上一个监听
刷新页面之前将state数据存到localstorage,
刷新之后 如果localstorage有东西,就返还给state
new Vue({
router,
store,
name: ‘App’,
created() {
//在页面加载时读取sessionStorage里的状态信息
//信息能否刷新 下次
if (sessionStorage.getItem(“store”)) {
this.
s
t
o
r
e
.
r
e
p
l
a
c
e
S
t
a
t
e
(
O
b
j
e
c
t
.
a
s
s
i
g
n
(
,
t
h
i
s
.
store.replaceState(Object.assign({}, this.
store.replaceState(Object.assign(,this.store.state, JSON.parse(sessionStorage.getItem(“store”))))
}
//在页面刷新时将vuex里的信息保存到sessionStorage里
//store里面的信息是最新的,可以保证数据OK
window.addEventListener(“beforeunload”, () => {
sessionStorage.setItem(“store”, JSON.stringify(this.$store.state))
})
},
// ---------------------
//本身没问题,无需做回退
// watch:{
// $router(newVal){
// this.$store.mutaion("changeTab",newVal.name)
// }
// },
render: h => h(App)
}).$mount('#app')