Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

irwan_up's avatar

difference between props and data

i'm still confused between data and props in a component, can someone help me ? because i can't pass data from my root vue to its component.

0 likes
5 replies
mehany's avatar
mehany
Best Answer
Level 13

Very good question, you got me to brush on this part of VueJS @irwan_up . It appears to be props are kind of data holders with no recursive setters and getters like in data , for example an array with some values is passed then shared with child components. The docs states here

all props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. ....

where data is much more than that. anything in the data will become "reactive" with the setters and getters that Vue adds.

Vue.js will recursively convert its properties into getter/setters to make it “reactive”. The object must be plain: native objects, existing getter/setters and prototype properties are ignored. It is not recommended to observe complex objects.
Note the last note " It is not recommended to observe complex objects " which interestingly will create setters and getters for the methods of the object being referenced.

A quick tip: try this.$parent with child components or use Vue dev tools to check, just to ensure that the parent of the child is what you expect.

What code is not allowing you to pass data to child components!?

2 likes
jimmck's avatar

props are properties that can be set in your HTML markup. You can access them in your Vue instance and expose getters and setters to them.

<!doctype html>
<html>
<!-- field template -->
<script type="text/x-template" id="listbox-template">
    <div id="complist">
        <select v-model="selected" v-on:change="onListSelect" id="list_box" :size="size">
            <option v-for="opt in selectOptions" v-bind:value="opt">{{opt.text}}</option>
        </select>
    </div>
</script>

<script>
    function ListOption(t, v) {
        this.text = t;
        this.value = v;
    }

    Vue.component('mylistbox', {
        el: function () {return '#complist' + this.id},
        //template: "#listbox-template",
        props: ['size', 'doselect', 'id'],
        data: function () {
            return {
                list: null,
                selected: -1,
                selectOptions: [],
                selectHandler: null
            }
        },
        ready: function () {
            this.list = $('#' + this.id);
        },
        created: function () {
            if (this.size === undefined) {
                this.size = 3;
            }
            if (this.doselect !== undefined) {
                this.selectHandler = this.doselect;
            }
            var lissa = this.makeTemplate($('#listbox-template').html(), 'list_box', this.id);
            console.log(lissa);
            this.$options.template = lissa;
            //this.list = $(("#" + this.id));
            //alert(this.id);
        },
        methods: {
            makeTemplate: function (temp, target, id) {
                var replacer = (new StringReplacer())
                        .add(target, new Replacement(id))
                        .add('complist', new Replacement(id, Replacement.APPEND, 'complist'));
                return replacer.replace(temp);
            },
            setSize: function (size) {
                this.size = size;
                this.list.prop('size', size);
            },
            getSize: function () {
                return this.size;
            },
            addOption: function (text, value) {
                var opt = new ListOption(text, value);
                this.selectOptions.push(opt);
            },
            clearSelection: function () {
                this.list.val("");
            },
            onListSelect: function (e) {
                //debugger;
                if (this.selectHandler && (this.selected != -1 || this.selected != null)) {
                    var opt = new ListOption(this.selected.text, this.selected.value);
                    if (typeof this.selectHandler === "function") {
                        //fyi('function...');
                        this.selectHandler(opt);
                    } else {
                        this.$parent[this.selectHandler].apply(this.$parent, [opt]);
                    }
                }
            },
            addSelectHandler: function (handler) {
                this.selectHandler = handler;
            },
            clearList: function () {
                this.selectOptions = [];
                this.selected = -1;
            },
            setSelected: function (s) {
                this.selected = this.selectOptions[s];
            },
            getSelected: function () {
                //debugger;
                if (this.selected != -1) {
                    return this.selected.value;
                }
                return -1;
            },
        }
    });
</script>
</html>

Creates a HTML Listbox object. It rewrites the component template to set the id that is passed in as a component property. This allows it to use JQuery to manipulate the listbox. You can toggle things like number of visible rows in the list box via setter.

1 like
irwan_up's avatar

@mehany thanks a lot for the explanation, i'll try that later. About the code that didn't work. I did a simple cheating solution to pass data to my-component through hidden input field, then took its value as props and bind it with v-model to my-component's data. it works, but i think it's not a good idea

jimmck's avatar

@mehany Thanks. Just use this learn different aspects of Vue. Its a pretty perfect JS lib IMHO. Still getting the hang and avoiding the $ methods until I understand what is going on. Such as name collisions between props and data for one.

Please or to participate in this conversation.