vuex和 localStorage 区别

vuex和 localStorage 区别

  1. 最重要的区别:vuex 中的数据是存储在内存中的,页面刷新就会丢失;而 localstorage 则是存储在计算机本地,刷新并不会丢失;sessionStorage 生存于应用会话期间。

  2. 应用场景:vuex用于组件之间的传值,(响应式的),localstorage则主要用于不同页面之间的传值(其他页面更新数据了,当前页面要刷新才能相应更新,非响应式的)

  3. 永久性:vuex全局变量存储,当刷新页面时vuex存储的值会丢失(存在内存里的,刷新了,当然会丢失),localstorage不会。

  4. 我觉得这里讲得更详细。

浏览器回退 刷新:

  1. 添加一个隐藏的input 标签,设置value=“no”
<input type="hidden" id="refreshed" value="no">
  1. 页面第一次加载的时候 执行refreshedId.value = ‘yes’, 随后在加载时执行refreshedId.value = ‘no’,并再重新加载一次。
onload = function () {
  var refreshedId = document.getElementById('refreshed')
  if (refreshedId.value === 'no') {
    refreshedId.value = 'yes'
  } else {
    refreshedId.value = 'no'
    location.reload()
  }
}

更改配置文件后 重启才能生效!

发布了50 篇原创文章 · 获赞 23 · 访问量 1226

猜你喜欢

转载自blog.csdn.net/qq_44698161/article/details/103304257