Because it's possible that localStorage.getItem('auth')
will return null
, while JSON.parse
requires a string.
You will need to do a null check before parsing the variable.
cosnt authRaw: string = localStorage.getItem('auth');
if(authRaw !== null){
const auth: boolean = JSON.parse(authRaw);
}
A simpler approach is to use ??
to add a fallback value as an alternative to localStorage.getItem('auth')
const auth:boolean = JSON.parse(localStorage.getItem('auth') ?? "false");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…