首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从pinia状态删除项目

从pinia状态删除项目
EN

Stack Overflow用户
提问于 2022-09-08 19:05:52
回答 1查看 56关注 0票数 0

我是vue的新手,我刚刚开始使用pinia。我想从array中删除一个项目,但它不起作用

这是我的店

代码语言:javascript
复制
import {defineStore} from 'pinia'

export interface ObjectDto {
  input: string,
}

interface ObjectDtoInterface {
  objects: Array<ObjectDto>
}

export const useSearchHistoryStore = defineStore('objectsStore', {
  state: (): ObjectDtoInterface => {
    return {
      objects: [] as ObjectDto[]
    }
  },
  actions: {
    add(dto: ObjectDto) {
      if (this.objects
        .filter(shd => dto.input === shd.input)
        .length === 0) {
        this.objects.unshift(dto)
      }
    },
    delete(obj: ObjectDto) {
      this.objects = this.objects.filter(e => !(e.input === obj.input))
    }
  }
})

下面是来自不同.ts文件的函数

代码语言:javascript
复制
function delete(obj: ObjectDto) {
  objectsStore.delete(obj)
}

add操作工作得很好,它将项添加到状态,但是当我尝试对一个项进行delete时,什么都不会发生。我传递给delete方法的数据是100%好的,因为我检查了很多次

EN

回答 1

Stack Overflow用户

发布于 2022-09-08 19:14:41

过滤器不改变原始对象,您需要重新定位。

代码语言:javascript
复制
delete(obj: ObjectDto) {
      this.objects = this.objects.filter(e => !(e.input === obj.input))
 }

更多信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73653964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档