简体   繁体   English

Vue深拷贝object不改变值

[英]Vue deep copy object not changing value

I have a vue component that has a deep copy of an asset called cachedAsset.我有一个 vue 组件,其中包含一个名为 cachedAsset 的资产的深层副本。 It has a value called toDelete to mark a file for soft deletion.它有一个名为 toDelete 的值来标记要软删除的文件。 I have the following code我有以下代码

markForDeletion(files) {
          const thisComponent = this;
          files.forEach((file) => {
              thisComponent.cachedAsset.files.find(f => file.id === f.id).toDelete = true;
          });
      }

this works as intended it changes the.toDelete to true and the file is filterd out in a process further down the line.这可以按预期工作,它将 the.toDelete 更改为 true,并且该文件在下一个过程中被过滤掉。

The issue that I am having is with restoring the file back to the component with the following code我遇到的问题是使用以下代码将文件恢复到组件

restoreFilesFromDeletion(items) {
          items.forEach((item) => {
              this.cachedAsset.files.find(f => item.id === f.id).toDelete = false;
          });
      }

With this code it should set the.toDelete back to false but it is not doing that and I get no errors or anything in the console.使用此代码,它应该将 the.toDelete 设置回 false,但它没有这样做,我在控制台中没有收到任何错误或任何内容。

Can anyone tell me why this is not updating the.toDelete back to false when executed.?谁能告诉我为什么执行时没有将 the.toDelete 更新回 false。?

Edit 1编辑 1

this is what I have now这就是我现在所拥有的

restoreFilesFromDeletion(items) {
          items.forEach((item) => {
              let files = this.cachedAsset.files.find(f => item.id === f.id)
              this.$set(files, 'toDelete', false)
          });
      }

it seems to be setting it but the true still does not change to false..它似乎正在设置它,但 true 仍然没有变为 false ..

am I still missing something?我还缺少什么吗? any further advice that can be given...可以提供任何进一步的建议...

It happens when data is deeply nested.当数据嵌套很深时,就会发生这种情况。 Try this Vue.delete .试试这个Vue.delete and also see Reactivity in Depth .并参见Reactivity in Depth

After re-analyzing the results with some console tests, I found that I had an error elsewhere in my code, once I fixed that the delete and restore are working properly.在用一些控制台测试重新分析结果后,我发现我的代码中的其他地方有错误,一旦我修复了删除和恢复工作正常。

Thank you for your assistance, it has given me more knowledge and insight on reactivity and the $set method.感谢您的帮助,它让我对反应性和$set方法有了更多的了解和见解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM