[英]Vue.js - Don't bind an object when 'copying it' to another data attribute
我有一個來自我的API的對象,當加載模態時,我需要將對象“復制”到另一個。
這有效:
this.servicesForm.services = this.team.services;
// New object // API object
現在的問題是我不希望在更新servicesForm.services對象時綁定並更新team.services對象。
我怎么做?
快速找到我的答案:
this.servicesForm.services = JSON.parse(JSON.stringify(this.team.services));
ES6解決方案是使用Object.assign:
this.servicesForm.services = Object.assign({}, this.team.services);
請注意,這只是一個淺表副本,如果您需要深層副本,則需要遞歸地應用此方法。
參考: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.