繁体   English   中英

如何在数据 Vue.js 中检查本地存储

[英]How to check localstorage in data Vue.js

我使用 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 中运行它。 所有代码在这里我真的不知道如何解决这个问题。 提前谢谢你!

我认为如果 localStorage 为空,您需要回退:

{
    el: '.row',
    data:{
        cap:undefined,
        ti:undefined,
        forLS:JSON.parse(localStorage.getItem("allData") || '{ "items":[] }'),
        count:JSON.parse(localStorage.getItem("count") || '-1' )
}

实际上这是你的小提琴的一个工作叉

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM