使用方法
Object.assign(this.$data, this.$options.data());
問題
如果data()中若使用了this來訪問props或methods,在重置完$data時,this.methodA和this.B爲undefined。
原因
和Vue實例的初始化相關。
1、new Vue的時候傳了一個對象,把該對象記爲options,Vue將options中自定義的屬性和Vue構造函數中定義的屬性合併爲vm.options.data()中的this指向vm.options下,所以this.methodA和this.B爲undefined。
解決方法
// 指定this
Object.assign(this.$data, this.$options.data.call(this));