Why Vue’s data should be written as a function


The components of Vue are reusable. After a component is created, it can be reused in multiple places, no matter how many times it is reused, thedataThey should be isolated from each other, so every time a component is reused,dataIt should be reused once, every timedataThe change should have no effect on the data of other reusable components.

In order to achieve this effect,dataIt can not be a simple object, but in the form of a function return value, so each component instance can maintain an independent copy of data without mutual influence.