[LayaAir 2.0]Laya.LocalStorage.getJSON空对象在微信小游戏中被处理为空字符串
var equipDataArray = Laya.LocalStorage.getJSON('equipDataArray');
如果getJSON为空,在LayaAir IDE中获取的是null,而在微信小游戏中获取的是'';
因此,上面这段代码在微信小游戏中执行会报错,提示weaponIndex是undefined对象的属性;
为了避免这个错误被触发,应该同时判断
if (equipDataArray === null || equipDataArray.length === 0)
这样在IDE和微信开发工具中就都可以正常运行了。
如果getJSON为空,在LayaAir IDE中获取的是null,而在微信小游戏中获取的是'';
getLocalStorage() {
//清除本地存储 调试时取消下面一行的注释
Laya.LocalStorage.clear();
//获取当前武器配置
var equipDataArray = Laya.LocalStorage.getJSON('equipDataArray');
console.log('equipDataArray ', equipDataArray, ' |');
if (equipDataArray === null) {
equipDataArray = [
{ icon: 'img/weapon/gun0.png', weaponIndex: 0, weaponGrade: 1 },
{ icon: 'img/weapon/gun1.png', weaponIndex: 1, weaponGrade: 1 },
{ icon: 'img/weapon/gun2.png', weaponIndex: 2, weaponGrade: 1 },
{ icon: 'img/weapon/gun3.png', weaponIndex: 3, weaponGrade: 1 },
{ icon: 'img/weapon/gun4.png', weaponIndex: 4, weaponGrade: 1 },
{ icon: 'img/weapon/gun5.png', weaponIndex: 5, weaponGrade: 1 },
];
Laya.LocalStorage.setJSON('equipDataArray', equipDataArray);
}
console.log('equipDataArray[4].weaponIndex = ', equipDataArray[4].weaponIndex);
}
因此,上面这段代码在微信小游戏中执行会报错,提示weaponIndex是undefined对象的属性;
为了避免这个错误被触发,应该同时判断
if (equipDataArray === null || equipDataArray.length === 0)
这样在IDE和微信开发工具中就都可以正常运行了。
没有找到相关结果
已邀请:
要回复问题请先登录
0 个回复