localstorage、sessionstorage 存储布尔值要注意
文章目录
localstorage、sessionstorage 存取数据方式:
存储字符、数字、布尔值等
存:localstorage.setItem('key', key-value);
取:localstorage.getItem('key');
存储对象
存:localstorage.setItem("key", JSON.stringift(key-value));
取:JSON.parse(localstorage.getItem('key'));
当 localstorage 或 sessionstorage 存储布尔值数据时,取到的数据变成了字符串'true' 'false',建议在存储该类型数据时将 value 设置为 0、1,取值时用Number(localstorage.getItem('key')),再进行后续判断操作。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论