在使用 cc.sys.localStorage.setItem(key, value); 的时候被坑了一下,我存进去的时候是 Boolean 类型,取出来的时候是 string 类型,我使用 Ts 定义的布尔类型的能保存 string 而且没有报错。最后在浏览器输出文字的颜色不一样使用 typeof 输出了一下类型才发现的。
解决方法:
public static getTypeUnlock(index: number) {
let key = `${index}`;
let unlock: boolean = false, item;
item = cc.sys.localStorage.getItem(key);
if (!item) {
StorageManager.setItem(key, false);
return false;
}
if (item == `false` || item == false) {
unlock = false;
}
if (item == `true` || item == true) {
unlock = true;
}
return unlock;
}
public static setItem(key: string, value: any) {
cc.sys.localStorage.setItem(key, value);
}
不知道大佬有没有更好的解决方法

