-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed bug: localStorage and json.parse()
- Loading branch information
Showing
4 changed files
with
63 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export function getLocal(key) { | ||
let localStorage = window.localStorage, | ||
valStr | ||
|
||
if(key !== undefined && key !== null) { | ||
valStr = localStorage.getItem(key) | ||
} else { | ||
return false | ||
} | ||
|
||
if(valStr !== 'undefined' && valStr !== 'null') { | ||
try { | ||
return JSON.parse(valStr) | ||
} catch(e) { | ||
console.log(`localStorage 的 ${key} 属性解析失败`) | ||
return false | ||
} | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
|
||
export function setLocal(key, val) { | ||
let localStorage = window.localStorage | ||
|
||
if(key !== undefined && key !== null) { | ||
try { | ||
localStorage.setItem(key, JSON.stringify(val)) | ||
} catch (e) { | ||
console.log(`localStorage 的 ${key}:${val}序列化失败`) | ||
return false | ||
} | ||
} else { | ||
console.log(`localStorage 的 ${key} 是非法值 undefined/null`) | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters