Why must vue component data be a function?

后端 未结 4 1982
感动是毒
感动是毒 2020-12-31 02:55

I am reading up on Vue components, and find their explanation of why data needs to be a function somewhat confusing:

The root instance



        
4条回答
  •  被撕碎了的回忆
    2020-12-31 03:59

    From my understanding of this, It's to save memory

    Many frameworks, such as Angular 2 or, (at times) React, make each instance of a component a separate object. This means that everything each component needs is initialized for every component. Normally though, you really only need to keep a component’s data separate for each initialization. Methods and such stay the same.

    Vue avoids that pitfall by having data be a function that returns an object. That allows separate components to have separate internal state without needing to fully re-instantiate the entire component. Methods, computed property definitions, and lifecycle hooks are created and stored only once, and run against every instance of a component.

    See this

提交回复
热议问题