vuex存数据,防止刷新数据丢失

 1 created() { 
 2   if (sessionStorage.getItem('store')) {
 3     this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem('store'))))
 4   }
 5   let isOnIOS = navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i);
 6   let eventName = isOnIOS ? 'pagehide' : 'beforeunload';
 7 
 8   window.addEventListener(eventName, () => {
 9     sessionStorage.setItem('store', JSON.stringify(this.$store.state));
10   })
11 }

猜你喜欢

转载自www.cnblogs.com/shoestrong/p/10826952.html