Javascript:localStorage.getItem(密钥)不工作

Javascript: localStorage.getItem(key) not working

我正在尝试使用本地存储来存储一些纹理对象,但它似乎不起作用。

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);
    }

这里我试图用key = "name" + i在本地存储中存储一个键值对,value是gettexture函数返回的纹理对象,但这似乎不起作用。

您不能直接将对象存储在localstorage 中。 一种解决方法是在存储对象之前将其字符串化,然后在检索它时对其进行解析:

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'));