vue中sessionStorage存储的用法和问题


存:

sessionStorage.setItem("signoutShow",this.signoutShow);
 
取:
this.signoutShow = sessionStorage.getItem('signoutShow');
 
vue中由于页面刷新会导致有些参数被清空,用sessionStorage存储数据一般是在  beforeMount() 挂载前取,  beforeUpdate() 更新渲染时存:
  beforeMount() {
    this.signoutShow = sessionStorage.getItem('signoutShow');
  },
  beforeUpdate() {
    sessionStorage.setItem("signoutShow",this.signoutShow);
  }

【注】sessionStorage 存储数据一般为对象,当存储值为 true/false 时,会发现存取都可以得到正确的值,但是页面渲染的结果不对,这是因为存储的类型为字符串类型,而 true/false 为bool 类型, sessionStorage.getItem('signoutShow'); 取值的时候会得到带引号的值,比如说"true" / "false" ,那么这时候就需要用 JSON.parse 转换一下类型就好了

  beforeMount() {
    var signoutBool = sessionStorage.getItem('signoutShow');
    this.signoutShow = JSON.parse(signoutBool);
  }

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM