我是vue的新手,我刚刚开始使用pinia。我想从array中删除一个项目,但它不起作用
这是我的店
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文件的函数
function delete(obj: ObjectDto) {
objectsStore.delete(obj)
}add操作工作得很好,它将项添加到状态,但是当我尝试对一个项进行delete时,什么都不会发生。我传递给delete方法的数据是100%好的,因为我检查了很多次
发布于 2022-09-08 19:14:41
过滤器不改变原始对象,您需要重新定位。
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
https://stackoverflow.com/questions/73653964
复制相似问题