Why must vue component data be a function?

后端 未结 4 2000
感动是毒
感动是毒 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条回答
  •  -上瘾入骨i
    2020-12-31 04:01

    It must be a function because otherwhise the data will be shared among all instances of the component, as objects are call by reference rather than call by value. This does not only happen when you reference a global object but also when data is an object itself. If data is a factory-function that returns an object this object will be created from scratch every time you mount a new instance of the component instead of just passing a reference to the global data.

提交回复
热议问题