Why does data in Vue need to return?

Why return?

data(){
    return {
        a:10,
        b:"hahahhahah"
    }
}

How to write the function: return a new object each time

Because if the Vue project is relatively large, when creating multiple components, the data of objects created without return will point to the same address, causing data pollution. If a new object is returned through function writing, the addresses of the objects are different, and the data are independent of each other. Avoid data pollution.

The specific reason why data is a function:

The objects in the vue file will be used as options to build component instances. When multiple identical components are referenced, they are actually created based on the same object. If data is an object, the data of these components will point to the same object because they will interfere with each other. Using functions, you can call the function generation during construction. Object, at this time the data between the components does not interfere with each other.

Guess you like

Origin blog.csdn.net/m0_60237095/article/details/134378853