简体   繁体   English

LocalStorage.getItem 值返回 null

[英]LocalStorage.getItem Value returns null

I am trying to toggle between a table and a graph.我正在尝试在表格和图表之间切换。 I have written a couple of functions save the state that the user switches to using localstorage.我编写了几个函数来保存用户切换到使用 localstorage 的状态。 But when I console.log localStorage.getItem, i see the value as null.但是当我 console.log localStorage.getItem 时,我看到该值为 null。 What am I missing?我错过了什么? This is Vanilla Javascript这是香草 Javascript

const data = document.querySelectorAll("button.data");
const dataTable = document.querySelector(".dataTable");
const dataGraph = document.querySelector(".dataGraph");

const ViewdataGraph = () => {
    dataTable.style.display = "none";
    dataGraph.style.display = "block";
    data.forEach(
        (element) => (element.innerText = "View data Table"));
   localStorage.setItem("dataViewType", "Graph");

}

const ViewdataTable = () => {
    dataGraph.style.display = "none";
    dataTable.style.display = "block";
    data.forEach(
        (element) => (element.innerText = "View data Graph"));
   localStorage.setItem("dataViewType", "table");

}

const Toggledata = () => {
    const dataType = localStorage.getItem("dataViewType");

    if (dataType) {
        if (dataType === "table") {
            ViewdataTable();

        } else if (dataType === "Graph") {
            ViewdataGraph();

        }
    }
};

 


window.addEventListener("DOMContentLoaded", (event) => {

    const data = document.querySelectorAll("button.data");
    data.forEach((element) =>
        element.addEventListener("click", (event) => ToggleData()),
    );
}

on your code only I see that you have 2 dots on this line of code localStorage..setItem("dataViewType", "table");仅在您的代码上,我看到您在这行代码localStorage..setItem("dataViewType", "table");上有两个点change to this localStorage.setItem("dataViewType", "table");改成这个localStorage.setItem("dataViewType", "table"); I hope this is your problem我希望这是你的问题

You have a typo in your ViewdataTable function.您的ViewdataTable函数中有错字。

localStorage.setItem("dataViewType", "table");

Also, there's a problem in Toggledata , when localStorage has no dataViewType item, the variable dataType will be null , hence no changes will be made to your view or localStorage .此外, Toggledata中存在问题,当 localStorage 没有dataViewType项时,变量dataType将为null ,因此不会对您的视图或localStorage进行任何更改。 I suggest using an array of views and storing the index of the active view.我建议使用视图数组并存储活动视图的索引。

const views = [
    { elem: dataTable, name: 'Table' },
    { elem: dataGraph, name: 'Graph' },
];

let viewIndex = -1;

const Toggledata = () => {
    views[viewIndex].elem.style.display = "none";
    viewIndex++;

    // wrapping
    if (viewIndex >= views.length) {
        viewIndex = 0;
    }

    const view = views[viewIndex];
    view.elem.style.display = "block";
    data.forEach(element => (element.innerText = `View data ${view.name}`));
    localStorage.setItem("dataViewType", String(viewIndex));
};

window.addEventListener("DOMContentLoaded", (event) => {
    const data = document.querySelectorAll("button.data");
    data.forEach((element) =>
        element.addEventListener("click", (event) => ToggleData()),
    );

    // use first view if dataViewType is not set
    viewIndex = Number(localStorage.getItem("dataViewType") || "0");

    // not a valid value
    if (Number.isNaN(viewIndex) || !(viewIndex in views)) {
        viewIndex = 0;
    }

    // maybe not necessary if HTML style sets display to none by default
    views.forEach(v => (v.elem.style.display = 'none'));
    views[viewIndex].elem.style.display = 'block';
}

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

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