[英]Add data to localStorage and retrieve it
I have the following: 我有以下几点:
<form onsubmit={ addToLocalStorage }>
</form>
this.items = []
addToLocalStorage(e) {
var input = e.target[0]
var todos = JSON.parse(localStorage.getItem('todos') || '[]')
var savedItems = this.items.push(input.value)
localStorage.setItem('todos', JSON.stringify(savedItems))
console.log(todos)
}
But the outputs only shows: 但是输出仅显示:
1
2
and so on. 等等。
What am I doing wrong? 我究竟做错了什么?
EDIT: 编辑:
console.log(localStorage.getItem('todos'))
Also outputs 1
, 2
but color black in the console (so I think as strings?). 还输出
1
, 2
,但黑色控制台(所以我觉得作为字符串?)。
.push()
returns the new array length. .push()
返回新的数组长度。
var a = [].push("a");
a = a.push("b");
a is now equal to 2 and that array is gone. 现在a等于2并且该数组消失了。
Here's what you want: 这就是您想要的:
this.items.push(value);
localStorage["someKey"] = JSON.stringify(this.items);
...
var loadedArray = JSON.parse(localStorage["someKey"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.