44

I have used data option in two ways. In first snippet data object contains a key value, however, in second data is a function. Is there any benefits of individuals.Not able to find relevant explanations on Vue.js Docs Here are two code snippets:

new Vue({
  el: "#app",
  data: {
      message: 'hello mr. magoo'
    }

});

new Vue({
  el: "#app",
  data() {
    return {
      message: 'hello mr. magoo'
    }
  }
});

Both are giving me the same output.

5
  • 9
    vuejs.org/v2/api/#Options-Data 5th paragraph of detail content is what you are looking for. Commented Jan 9, 2018 at 20:55
  • 1
    so ,data() { return { message: 'hello' } and data: function() { return { message: 'hello ' } both are same? Commented Jan 9, 2018 at 20:57
  • 5
    yes, data () {} is ES6, data: function is ES5 way of same code. Commented Jan 9, 2018 at 21:00
  • Thanks mate. Bdw where can I find detailed instrction on ES6 and ES5 specs? It will be great if you provide any. Commented Jan 9, 2018 at 21:01
  • 2
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 9, 2018 at 21:07

2 Answers 2

51

It seems as though the comments on your question missed a key point when considering your specific code example.

In a root Vue instance i.e. constructed via new Vue({ ... }), you can simply use data: { ... } without any problems. The issue is when you have reusable components that are defined via Vue.component(...). In these instances, you need to either use data() {return { ... };} or data: function() {return { ... };}.

The reason for this is to ensure that for each individual instance of the reusable child component, there is a unique object containing all of the data being operated on. If, in a child component, you instead use data: { ... }, that same data object will be shared between the child components which can cause some nasty bugs.

Please review the corresponding section of the Vue.js documentation for more information regarding this problem.

Sign up to request clarification or add additional context in comments.

Comments

0

[Vue warn]: The "data" option should be a function that returns a per-instance value in component definitions.

so initiating a new vue instance dose not matter between data:{} as a object or data(){return{}} or data:function(){return{}}.

It matters when it comes to component lets try an example:

<body>
    <div id="app">
        <counter></counter>
        <counter></counter>
    </div>
    <script>
         Vue.component('counter', {
             template: '<button v-on:click="counter += 1">{{ counter }}</button>',
             data: {
                 counter:0
             }
         });
    </script>

Output:

[Vue warn]: The "data" option should be a function that returns a per-instance value in component definitions.

Now lets watch in vue object:

<body>
    <div id="app">
        <button v-on:click="counter += 1">{{ counter }}</button>
        <button v-on:click="counter += 1">{{ counter }}</button>
    </div>
    <script>

        new Vue({
            el: '#app',
            /*data() {
                return {
                    counter:0
                }
            },*/
            //or (output same)
            /*data: function () {
                return {
                    counter: 0
                }
            }*/
            //or (output same)
            data:{
                counter:0
            }

        });
    </script>

</body>

//Now let's try data as a function in the component to reuse same component over and over again.

<body>
    <div id="app">
        <counter></counter>
        <counter></counter>
        <counter></counter>
    </div>
    <script>

        Vue.component('counter', {
            template: '<button v-on:click="counter += 1">{{ counter }}</button>',
            data: function () {
                return {
                    counter: 0
                }
            }
        })

        new Vue({
            el: '#app'
        });
    </script>

</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.