JSON.parse error:“Type ‘null‘ is not assignable to type ‘string‘“
使用localStorage将Vue3中data声明的一个数组进行解析时。由于定义如下:
notFinish: JSON.parse(localStorage.getItem('notFinish')),
出现了如下错误:
Type 'null' is not assignable to type 'string'
解决方法:
可以“或”一个空数组字符串,即改成如下形式:
notFinish: JSON.parse(localStorage.getItem('notFinish')) || '[]',
还没有评论,来说两句吧...