question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

#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:open
  • Created 5 years ago
  • Reactions:12
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

32reactions
colthreepvcommented, Oct 30, 2018

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

3reactions
BehindTheMathcommented, Sep 17, 2018

localData doesn’t follow the change of data, in my example, because data is a primitive value.

I understand. My question was if localData would be reactive, and would the template update if localData was assigned a new value. I see now that it is, so never mind that.

Regardless, I still think that

@Data() localData = this.data;

is simpler and nicer than

    localData = ''

    created() {
        this.localData = this.data
    }

but that’s just my opinion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decorator pattern - Wikipedia
In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, ...
Read more >
dataclasses — Data Classes — Python 3.11.1 documentation
The dataclass() decorator examines the class to find field s. A field is defined as a class variable that has a type annotation....
Read more >
Decorator - Refactoring.Guru
Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that...
Read more >
Decorators in Python: Fundamentals for Data Scientists
Decorators in Python is used to extend the functionality of a callable object without modifying its structure.
Read more >
Python Decorators Tutorial: What is a Decorator? - DataCamp
If you would like to learn about functions, take DataCamp's Python Data Science Toolbox (Part 1) course. A decorator is a design pattern...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found