简体   繁体   English

Javascript:localStorage.getItem(key)不起作用

[英]Javascript: localStorage.getItem(key) not working

I am trying to use local storage to store some texture objects but it doesn't seem to work. 我试图使用本地存储来存储一些纹理对象,但它似乎不起作用。

for (var i = 0; i < 6; i++) {
    localStorage.setItem("name" + i, Transition.blurPano.getTexture(path + img_name[i] + ".jpg", dfrd[i], true, i));

    console.log(localStorage.getItem("name" + i) == Transition.blurPano.getTexture(path + img_name[i] + ".jpg", dfrd[i], true, i));

        Transition.blurPano.mesh.material.materials[i].map = localStorage.getItem("name" + i);
    }

Here I am trying to store a key value pair in the local storage with key = "name" + i and value is the texture object which is returned by the gettexture function, but this doesn't seem to work. 在这里,我尝试使用key = "name" + i在本地存储中存储键值对,value是gettexture函数返回的纹理对象,但这似乎不起作用。

You cannot store objects in localstorage directly. 您无法直接在localstorage中存储对象。 A workaround can be to stringify your object before storing it, and later parse it when you retrieve it: 解决方法可以是在存储对象之前对其进行字符串化,然后在检索对象时对其进行解析:

var name = { 'first': 1, 'second': 2, 'third': 3 };

// Put the object into storage
localStorage.setItem('name', JSON.stringify(name));

// Retrieve the object from storage
var retrievedObject = JSON.parse(localStorage.getItem('name'));

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

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