vuejs / vue-class-component

ES / TypeScript decorator for class-style Vue components.
MIT License
5.81k stars 429 forks source link

Cannot reset component data to initial state with this.$options.data() #448

Closed Alona2156 closed 4 years ago

Alona2156 commented 4 years ago

The line that provokes problem:

Object.assign(this.$data, this.$options.data());

I see that when my project loads this function belonging to vue-class-component gets called

function collectDataFromConstructor(vm, Component) {}

vm is a VueComponent instance.

On the other hand when I try to reset component to initial state with this.$options.data() the function collectDataFromConstructor is executed with vm argument of different type. Instead of being a VueComponent instance it is VueComponent.$options. And the collectDataFromConstructor function cannot handle this.

When it arrives here

if (vm.$options.props) {}

It cannot read vm.$options.props because vm is not a component instance, it's already $options part.

ktsn commented 4 years ago

Please use .call.

this.$options.data.call(this)