vue 中經常定義很多data ,在用戶進行一些操作后,需要講data中的某個對象定義為初始值
例如
1 form: { 2 title: '', 3 describe: '', 4 inspectionCategoryIdList: [], 5 enterpriseId: '', 6 selectInc: { 7 name: '' 8 } 9 } 10 }
這樣一個復雜的對象,我們需要講他們全部定義為初始值
也許我我們可以這么寫
1 this.form = { 2 title: '', 3 describe: '', 4 inspectionCategoryIdList: [], // 任務ID 5 enterpriseId: '', 6 selectInc: { 7 name: '' 8 } 9 } 10 }
但是開發過程中,經常對這個對象進行變動,難免遺忘恢復初始值的方法,這樣會導致一些新增的key為 undefined 從后台獲取參數並添加的時候,會無法賦值
這時候我們可以用 Object.assign 淺拷貝這樣的一個對象
Object.assign(this.form, this.$options.data().form)
不僅節省了計算的時間,也節省了內存
