@Data decorator
See original GitHub issue#36 requests a new @Data
decorator to set properties as reactive. It is resolved there as unneeded, since you can just initialize the value as null
.
However, there is another situation for which a @Data
decorator would be useful: if you want to initialize the value of the data member to the value of a prop. ( A reason to do this would be so you can have a variable that initially mirrors the prop, but you can change the value later).
For example:
<template>
<input v-model="localData"></input>
</template>
<script="ts">
import {Vue, Prop} from "vue-property-decorator";
@Component({})
export default MyComponent extends Vue {
@Prop(String) data!: string;
localData: string = data;
}
</script>
Currently, since local class member declarations and initializations are done in the class constructor, at that point the value of the props are still undefined
, so that’s what this component would display.
The current workaround for this is to use a data
method in the Component
options, since when Vue calls data()
, the props are already initialized:
@Component({
data() {
return {
localData: this.data,
}
},
})
If there was a @Data
decorator that would convert something like this:
@Data() localData: string = data;
to a data
method as above, that would make the code cleaner.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:12
- Comments:11 (3 by maintainers)
Top GitHub Comments
Bump, this solution it’s not adequate in my opinion.
This library is meant to be a little bit of sugar syntax on top of the very simple Vue interface, it should not overcomplicate basic things, and
data
is the simplest Vue Component part.Please reconsider
I understand. My question was if
localData
would be reactive, and would the template update iflocalData
was assigned a new value. I see now that it is, so never mind that.Regardless, I still think that
is simpler and nicer than
but that’s just my opinion.