javascript - 按索引删除 localStorage vuejs 数组中的文章

标签 javascript vue.js local-storage

我无法删除 localStorage 中的项目。我创建了一个表来存储添加的项目。我想通过索引或 ID(如果可能)删除一篇文章。谢谢你的帮助

deleteOption(){
      this.test = JSON.parse(localStorage.getItem('test'))
      this.test.splice(index, 1)
},

test:[
   {
    id:1,
    item: test item 1
   },
   {
    id:2,
    item: test item 2
   },
   {
    id:3,
    item: test item 3
   },   
]


addItem () {

localStorage.setItem("test",JSON.stringify(this.test))

}


最佳答案

localstorage getItem 方法返回一个应解析为数组的字符串:

  this.test = JSON.parse(localStorage.getItem('test'))
  this.test.splice(index, 1)

关于javascript - 按索引删除 localStorage vuejs 数组中的文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67711808/

相关文章:

javascript - 尝试用 JavaScript 编写计算器

javascript - 为所有数组元素调用函数,而不将 item 作为参数传递

javascript - 在页面加载时随机定位 div

select - Vue 多选异常行为

rest - Vuejs 中的异步和等待

javascript - 强制本地存储事件在同一窗口中触发

javascript - polymer - 应用程序存储 : Remove entry and update storage

Javascript 链接帮助创建游标

javascript - 在 Vue.js 中如何使用多个路由 View ,其中一个在另一个组件中?

node.js - 如何从缓存位置:本地存储中检索msal对象?