JSON.parse error:“Type ‘null‘ is not assignable to type ‘string‘“

末蓝、 2022-10-29 13:20 120阅读 0赞

使用localStorage将Vue3中data声明的一个数组进行解析时。由于定义如下:

  1. notFinish: JSON.parse(localStorage.getItem('notFinish')),

出现了如下错误:

  1. Type 'null' is not assignable to type 'string'

解决方法:
可以“或”一个空数组字符串,即改成如下形式:

  1. notFinish: JSON.parse(localStorage.getItem('notFinish')) || '[]',

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读