Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in Vue between data() and adding data in created()

Is there a difference between the following? I've seen examples doing both and am unsure why you would choose one over the other.

Vue.component('test', {

        data() {
           return { myDataA: 10 };
        }

        //vs

        created() {
           this.myDataB = 10;
        }
    }
like image 855
Emanon Avatar asked Feb 18 '26 10:02

Emanon


1 Answers

Variables set in created() on this will not be reactive. In order for them to be reactive, you must define them in the object returned by data().

Example (note how the text does not change in the output): https://jsfiddle.net/oyf4quyL/

like image 110
ffxsam Avatar answered Feb 20 '26 00:02

ffxsam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!