Skip to content

Data function is called twice not once. #2687

@audinue

Description

@audinue

Vue.js version

1.0.21

Reproduction Link

http://jsbin.com/doxonuviwe/edit?html

Steps to reproduce

  • Register a Vue component with data function.
Vue.component('my-hello', {
    data: function() {
        console.log('hello')
        return {};
    }
});

new Vue({
    el: 'body'
});
  • Instantiate the component.
<my-hello></my-hello>
<my-hello></my-hello>
  • You will see in the console (DevTool):
hello
hello
hello
hello

What is Expected?

Data function should be called twice since there is only two instances of Vue component.

What is actually happening?

Data function called four times.

Quick Fix

Set the data in the created hook.

Vue.component('my-component', {
    created: function() {
        this.$data  = {}; // Set your data here.
    },
});

Hope this helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions