我有一个create-post.js
文件,它将输入的数据保存到本地存储中
var newPost = {
userName: username,
date: date.toLocaleDateString(),
postTitle: title,
postText: text,
storedImage: imgData
}
localStorage.setItem("newPost", JSON.stringify(newPost));
在这里,我尝试将对象推入数组并渲染项目
let local = JSON.parse(localStorage.getItem('newPost'));
var dataImage = localStorage.getItem('imgData');
let postArray = []
postArray.push(local);
console.log(postArray)
for (let i = 0 ; i < postArray.length ; i++){
this.innerHTML += `///rendering stuff`
但是,当我创建新帖子时,数组中仅显示最新输入。密钥名称有问题吗?如果每个帖子都需要唯一的键名,我该怎么做?我的模板字符串出问题了吗?(下)
模板字符串
<div class="post-preview">
<h6 id="postedBy">Posted By ${local.userName}</h6>
<p id="date">${local.date}</p>
<a href="#modal${local.postTitle}"><h4>${local.postTitle}</h4></a>
<div id="modal${local.postTitle}" class="modal">
<div class="modal-content">
<a href="#"><span class="close">×</span></a>
<p>${local.postText}</p>
<img src="" class="postedImage"/>
</div>
</div>
</div>