Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the reason why data uses return to return data in vue

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "the reason why data in vue uses return to return data", so the editor summarizes the following content to you, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "data in vue to use return to return data" article.

Reason: data that is not wrapped in return will be visible globally in the project, resulting in variable contamination, while variables in the data wrapped with return will only take effect in the current component and will not affect other components.

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

Official:

When a component is defined, the data must be declared as a function that returns an initial data object, because the component may be used to create multiple instances. If data

If it is still a pure object, all instances will share and reference the same data object! By providing the data function, we can call data every time we create a new instance

Function to return a completely new copy of the original data object.

Why does data need to use return to return data in a project?

Data that is not wrapped with return will be visible globally in the project and will cause variable contamination; variables in the data wrapped with return will only take effect in the current component and will not affect other components.

When a component is defined, the data must be declared as a function that returns an initial data object, because the component may be used to create multiple instances. If data

If it is still a pure object, all instances will share and reference the same data object! By providing the data function, each time a new instance is created, we can call the data function to return a new copy of the original data object.

Analogies and reference data types. If we don't use function return, the data of each component is the same address of memory, that one data changes and the other changes, which is certainly not what we want. Using function return is tantamount to declaring new variables, independent of each other, so there will be no such problem; js assigns object objects to the same memory address directly. So this approach is adopted for the independence of the data of each component.

If it is not a component, the normal data can be written as an object directly. For example, two calculators in the same component share data: {num: 0}. Sharing objects directly in JS will cause reference passing, that is, all numb values will be changed after the add and subtract button is pressed, so function is used here to return one object instance at a time.

The above is about the article about "what is the reason why data uses return to return data in vue". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report