无法读取未定义(读取 '$set')vuejs 的属性



所以当文档被删除时,我试图重新排序文档数组。我的文档数组看起来像这样:

[
{
"id": 1,
"name": "document1"
"document_number": 1,
},
{
"id": 30,
"name": "document2"
"document_number": 2,
},
{
"id": 4,
"name": "document3"
"document_number": 3,
},
{
"id": 4,
"name": "document4"
"document_number": 4,
}
]

现在如果用户删除"document2"我想被动地改变document_number对于document3,document4到"2","3"

我正在尝试:

reorderDocuments(document_number){
//filtering all documents that have "document_number" higher then the deleted document
const filteredDocuments = this.Documents.filter(function (e) {
return e.document_number > document_number;
});
//the filtering works as intended so now I use a for loop to loop thru all those documents
for(var i = 0; i < filteredDocuments.length; i++){
//here i am getting index of the document that i want to change
this.index = this.Documents.findIndex(x => x.id === filteredDocuments[i].id);
//the new number that should take place for document_number
const newNumber = filteredDocuments[i].document_number - 1;
//then im using $set to set that new number for the document
//here is where i get an error
this.Documents.document_number.$set(this.index, newNumber);
}
},

什么错了吗?

错误:

TypeError: Cannot read properties of undefined (reading '$set')

vm.$setthis.$set实例方法,该方法是全局Vue.set的别名。一般我们用它来设定vuex状态。因此我建议您使用

this.Documents[this.index]. document_number = newNumber;