vue使用assign巧妙重置data數據方式
使用assign巧妙重置data數據
由於Object.assign()有上述特性,所以我們在Vue中可以這樣使用:
Vue組件可能會有這樣的需求
在某種情況下,需要重置Vue組件的data數據。此時,我們可以通過this.$data獲取當前狀態下的data,通過this.$options.data()獲取該組件初始狀態下的data。
然後隻要使用Object.assign(this.$data, this.$options.data())就可以將當前狀態的data重置為初始狀態
重置data的數據為初始狀態
1. 逐個賦值
<span style="color:#000000"><code class="language-js"><span style="color:#99cc99">...</span> <span style="color:#6699cc">data</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span> <span style="color:#cc99cc">return</span> <span style="color:#999999">{</span> name<span style="color:#999999">:</span> <span style="color:#99cc99">''</span><span style="color:#999999">,</span> sex<span style="color:#999999">:</span> <span style="color:#99cc99">''</span><span style="color:#999999">,</span> desc<span style="color:#999999">:</span> <span style="color:#99cc99">''</span> <span style="color:#999999">}</span> <span style="color:#999999">}</span> <span style="color:#99cc99">...</span> <span style="color:#999999">// 逐個賦值</span> <span style="color:#cc99cc">this</span><span style="color:#999999">.</span>name <span style="color:#99cc99">=</span> <span style="color:#99cc99">''</span> <span style="color:#cc99cc">this</span><span style="color:#999999">.</span>sex <span style="color:#99cc99">=</span> <span style="color:#99cc99">''</span> <span style="color:#cc99cc">this</span><span style="color:#999999">.</span>desc <span style="color:#99cc99">=</span> <span style="color:#99cc99">''</span> </code></span>
這個方法比較笨,當然也可以實現效果,但是一個一個去重新賦值比較麻煩而且代碼看起來也會比較亂。
下面這個方法肯定是你喜歡的,一行代碼搞定~
2. 使用Object.assign()
MDN關於該方法的介紹:Object.assign() 方法用於將所有可枚舉屬性的值從一個或多個源對象復制到目標對象。它將返回目標對象。
用法: Object.assign(target, …sources)
第一個參數是目標對象,第二個參數是源對象,就是將源對象屬性復制到目標對象,返回目標對象
其中就是將一個對象的屬性copy到另一個對象
vue中:
this.$data
獲取當前狀態下的datathis.$options.data()
獲取該組件初始狀態下的data
所以,下面就可以將初始狀態的data復制到當前狀態的data,實現重置效果:
Object.assign(this.$data, this.$options.data())
當然,如果你隻想重置data中的某一個對象或者屬性:
this.form = this.$options.data().form
擴展
Object.assign(target, …sources) 方法還可以用來合並對象:
<span style="color:#000000"><code class="language-js"><span style="color:#cc99cc">const</span> o1 <span style="color:#99cc99">=</span> <span style="color:#999999">{</span> a<span style="color:#999999">:</span> <span style="color:#f99157">1</span> <span style="color:#999999">}</span><span style="color:#999999">;</span> <span style="color:#cc99cc">const</span> o2 <span style="color:#99cc99">=</span> <span style="color:#999999">{</span> b<span style="color:#999999">:</span> <span style="color:#f99157">2</span> <span style="color:#999999">}</span><span style="color:#999999">;</span> <span style="color:#cc99cc">const</span> o3 <span style="color:#99cc99">=</span> <span style="color:#999999">{</span> c<span style="color:#999999">:</span> <span style="color:#f99157">3</span> <span style="color:#999999">}</span><span style="color:#999999">;</span> <span style="color:#cc99cc">const</span> obj <span style="color:#99cc99">=</span> Object<span style="color:#999999">.</span><span style="color:#6699cc">assign</span><span style="color:#999999">(</span>o1<span style="color:#999999">,</span> o2<span style="color:#999999">,</span> o3<span style="color:#999999">)</span><span style="color:#999999">;</span> console<span style="color:#999999">.</span><span style="color:#6699cc">log</span><span style="color:#999999">(</span>obj<span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#999999">// { a: 1, b: 2, c: 3 }</span> console<span style="color:#999999">.</span><span style="color:#6699cc">log</span><span style="color:#999999">(</span>o1<span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#999999">// { a: 1, b: 2, c: 3 }, 註意目標對象自身也會改變。</span> </code></span>
如果對象中含有相同屬性,取最後一個屬性:
<span style="color:#000000"><code class="language-js"><span style="color:#cc99cc">const</span> o1 <span style="color:#99cc99">=</span> <span style="color:#999999">{</span> a<span style="color:#999999">:</span> <span style="color:#f99157">1</span><span style="color:#999999">,</span> b<span style="color:#999999">:</span> <span style="color:#f99157">1</span><span style="color:#999999">,</span> c<span style="color:#999999">:</span> <span style="color:#f99157">1</span> <span style="color:#999999">}</span><span style="color:#999999">;</span> <span style="color:#cc99cc">const</span> o2 <span style="color:#99cc99">=</span> <span style="color:#999999">{</span> b<span style="color:#999999">:</span> <span style="color:#f99157">2</span><span style="color:#999999">,</span> c<span style="color:#999999">:</span> <span style="color:#f99157">2</span> <span style="color:#999999">}</span><span style="color:#999999">;</span> <span style="color:#cc99cc">const</span> o3 <span style="color:#99cc99">=</span> <span style="color:#999999">{</span> c<span style="color:#999999">:</span> <span style="color:#f99157">3</span> <span style="color:#999999">}</span><span style="color:#999999">;</span> <span style="color:#cc99cc">const</span> obj <span style="color:#99cc99">=</span> Object<span style="color:#999999">.</span><span style="color:#6699cc">assign</span><span style="color:#999999">(</span><span style="color:#999999">{</span><span style="color:#999999">}</span><span style="color:#999999">,</span> o1<span style="color:#999999">,</span> o2<span style="color:#999999">,</span> o3<span style="color:#999999">)</span><span style="color:#999999">;</span> console<span style="color:#999999">.</span><span style="color:#6699cc">log</span><span style="color:#999999">(</span>obj<span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#999999">// { a: 1, b: 2, c: 3 } 屬性取最後一個對象的屬性</span></code></span>
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- 帶你領略Object.assign()方法的操作方式
- javascript對象的多種合並方式詳解
- javascript之Object.assign()的痛點分析
- JavaScript 賦值,淺復制和深復制的區別
- Vue.$set 失效的坑 問題發現及解決方案