Reactjs 通过保存在会话存储中的对象进行解析
因此,我分配了一个具有两个属性的对象,第一个属性“items”是保存在会话存储中的对象数组Reactjs 通过保存在会话存储中的对象进行解析,reactjs,ecmascript-6,react-props,Reactjs,Ecmascript 6,React Props,因此,我分配了一个具有两个属性的对象,第一个属性“items”是保存在会话存储中的对象数组 [{"id": 0, "name": "example"}] 第二个只是一个字符串 "this is an example" 当I console.log postingItems正常工作时,我发现该对象已合并 method = (e) => { const postingItems = Object.assign({}, { items: sessionSt
[{"id": 0, "name": "example"}]
第二个只是一个字符串
"this is an example"
当I console.log postingItems正常工作时,我发现该对象已合并
method = (e) => {
const postingItems = Object.assign({}, {
items: sessionStorage.getItem("items1"),
method: sessionStorage.getItem("method")
});
sessionStorage.setItem("items", postingItems)
console.log(postingItems)
}
但是当我在会话存储中保存postingItems,然后在控制台[Object]上显示console.log时
sessionStorage.setItem("finalItems", postingItems);
console.log(sessionStorage.getItem(finalItems));
您需要首先对对象进行字符串化,然后在加载/获取时进行解析
sessionStorage.setItem("finalItems", JSON.stringify(postingItems));
console.log(JSON.parse(sessionStorage.getItem("finalItems")));
您需要首先对对象进行字符串化,然后在加载/获取时进行解析
sessionStorage.setItem("finalItems", JSON.stringify(postingItems));
console.log(JSON.parse(sessionStorage.getItem("finalItems")));