Resetting VueJS data properties to initial values
If you really need to reset all of your data properties by firing the data()
method again, you can do that like so:
methods: { reset() { Object.assign(this.$data, this.$options.data.call(this)); }}
The this
variable in the this.$options.data
is referencing the options, not the vue component instance. That's why the localvar
property was undefined
. So if you're calling it from the vue instance, you'll need to give it a reference to this
via the function's call()
method.
But, in most circumstances, I would just assign the value directly instead of calling Object.assign
:
methods: { reset() { this.localvar = this.propvar; }}