访问页面的时候出现空白,查看控制台出现SyntaxError:JSON.parse:unexpected character at line 1 column 1 of the JSON data,这应该是JSON.parse导致的。
修复前的代码
static getItem(key: string): any {
const item = localStorage.getItem(key);
if (item) {
return JSON.parse(item);
}
return null;
}
修复后代码
static getItem(key: string): any {
const item = localStorage.getItem(key);
if (item&&typeof item == 'string' &&item.indexOf('{') == 0) {
return JSON.parse(item);
}
return null;
}