本地存储

235 阅读1分钟

localStorage--永久缓存

需要手动删除

  • setItem(‘名字’,‘值’)

    设置localstorage里面的内容
    
  • getItem(‘名字’)

    获取localstorage里面的存储的name数据
    
  • removeItem('名字',‘值’)

    删除localstorage里面的item成员
    
  • clear()

    清除所有数据
    

sessionStorage--会话缓存

关闭浏览器没有了

  • setItem(‘名字’,‘值’)

    设置sessionStorage里面的内容
    
  • getItem(‘名字’)

    获取sessionstorage里面的name数据
    
  • removeItem('名字',‘值’)

    删除sessionstorage里面的itme成员
    
  • clear()

    清除所有数据
    

区别

  • localStorage

    永久存储,除非手动删除
    
  • sessionStorage()

    会话存储,关闭浏览器消失
    

共同点

  • 只能存储字符串格式

存储多个(数组/对象)数据

  • 存数据

    localStorage.setItem("rowVal", JSON.stringify(rowVal))
    
  • 获取数据

    JSON.parse(localStorage.getItem("rowVal"))