-
this.$data
Vue 实例观察的数据对象 -
this.$options.data()
当前 Vue 实例的初始化data选项 -
Object.assign(target, ...sources)
object.assign()主要用于对象合并,将源对象中的属性复制到目标对象中,他将返回目标对象。有同名属性的话,后面的属性值会覆盖前面的属性值。
<template>
<div id="app">
<div>{{ num }}</div>
<button @click="reset">重置</button>
</div>
</template>
<script>
export default {
data() {
return {
num: 100,
a: 1,
b: 2,
}
},
methods: {
reset() {
Object.assign(this.$data, this.$options.data());
}
}
}
</script>
注意事项:如果你的vue实例的data方法里通过this引用了其他vue实例上的方法/属性等,需要改成下面这样,修复this指向问题导致的报错。
Object.assign(this.$data, this.$options.data.call(this));
网友评论