From c136b1f4ca0be4ea86634c1cf0318420f7d35c97 Mon Sep 17 00:00:00 2001 From: "liu.xiaofeng@ebiz-digits.com" Date: Thu, 26 Oct 2023 15:55:40 +0800 Subject: [PATCH] =?UTF-8?q?vuex=20=E4=B8=ADstore=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=20=E9=A1=B5=E9=9D=A2=E5=88=B7=E6=96=B0=20ios?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=AF=BC=E8=87=B4=E6=95=B0=E6=8D=AE=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E9=97=AE=E9=A2=98=E5=85=BC=E5=AE=B9=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/App.vue b/src/App.vue index def7824fc..e10d0881c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -28,13 +28,30 @@ export default { }, created () { // 在页面加载时读取sessionStorage - if (sessionStorage.getItem('store')) { - this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem('store')))) + // if (sessionStorage.getItem('store')) { + // this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem('store')))) + // } + // // 在页面刷新时将store保存到sessionStorage里 + // window.addEventListener('beforeunload', () => { + // sessionStorage.setItem('store', JSON.stringify(this.$store.state)) + // }) + //ios刷新时vuex信息保留 + let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 + if (isiOS) { + //在页面刷新时将vuex里的信息保存到缓存里 + window.addEventListener("pagehide", () => { + localStorage.setItem("store", JSON.stringify(this.$store.state)) + }) + //在页面加载时读取localStorage里的状态信息 + localStorage.getItem("store") && this.$store.replaceState(Object.assign(this.$store.state,JSON.parse(localStorage.getItem("store")))); + } else { + //在页面刷新时将vuex里的信息保存到缓存里 + window.addEventListener("beforeunload", () => { + localStorage.setItem("store", JSON.stringify(this.$store.state)) + }) + //在页面加载时读取localStorage里的状态信息 + localStorage.getItem("store") && this.$store.replaceState(Object.assign(this.$store.state,JSON.parse(localStorage.getItem("store")))); } - // 在页面刷新时将store保存到sessionStorage里 - window.addEventListener('beforeunload', () => { - sessionStorage.setItem('store', JSON.stringify(this.$store.state)) - }) }, mounted(){ },