报错:Failed to execute 'setItem' on 'Storage': Setting the value of 'address' exceeded the quota.
场景:对常用地址做本地存储,减少后端请求
异常:在localStorage里面没有找到存储的数据,刷新页面重新请求接口
排查:浏览器console输出报Failed to execute 'setItem' on 'Storage': Setting the value of 'address' exceeded the quota.查找资料发现是超出单条最大长度导致,
资料查询:发现此问题是超出了localStorage的最大长度导致的,另查找资料发现最大存储为5M;
撰写代码:清除存储数据的null和空字符串等无用数据,节省存储占用;问题暂时解决
反思:在使用本地存储的时候清除不必要的数据,防止出现空数据占用空间
获取最大存储的方法之一使用累加的方法,累加方法如下,可以验证你的剩余存储空间:
let str = "1234567890";
localStorage.setItem("str",str)
let timer = setInterval(()=>{
str+=localStorage.getItem("str");
console.log(str.length/(1024*1024),str.length);
try{
localStorage.setItem("str",str);
}catch (e) {
clearInterval(timer)
}
},20)