我使用Vue js和localstorage(LS)制作了todolist。一切都还可以,但是当我尝试在LS免费的其他浏览器中运行时,我遇到了错误Cannot read property 'items' of null"
。我必须先检查LS才能使用它,但是我正在读数据中的LS,我无法检查LS。
我的部分代码:
var app = new Vue({
el: '.row',
data:{
cap:undefined,
ti:undefined,
forLS:{
items:[]
},
count:-1,
forLS:JSON.parse(localStorage.getItem("allData")),
count:JSON.parse(localStorage.getItem("count"))
}
我尝试在函数中读取LS,但我无法在Data中运行它。 所有代码HERE 我真的不知道如何解决这个问题。提前谢谢!
答案 0 :(得分:3)
我认为如果localStorage为空,你需要一个后备:
{
el: '.row',
data:{
cap:undefined,
ti:undefined,
forLS:JSON.parse(localStorage.getItem("allData") || '{ "items":[] }'),
count:JSON.parse(localStorage.getItem("count") || '-1' )
}
实际上这里是你小提琴的working fork