In Vue, setup is used to encapsulate reuse; setup is designed to use combined APIs. When components become larger, the list of logical concerns will also grow, making the components difficult to read. and understanding, and through setup, this part can be extracted into a function, so you don't need to care about the logic of this part.
The operating environment of this article: Windows 10 system, Vue version 2.9.6, DELL G3 computer.
What is the use of setup in vue3?
setup is designed to use combined api
Why not use the options of the previous component
data, computed, methods, watch organizational logic is valid in most cases. However, as our components grow larger, the list of logical concerns also grows. This can result in components that are difficult to read and understand, especially for those who didn't write them in the first place. Through setup, this part can be extracted into a function, so that other developers do not need to care about this part of the logic.
The position of setup in the vue life cycle
setup is located before created and beforeCreated, used to replace created and beforeCreated, but this cannot be accessed in the setup function. In addition, the entire life cycle can be operated through the following hooks in the setup
What parameters can setup receive?
setup can accept props and context. Since props is responsive data, it cannot be deconstructed and assigned directly. Context is not responsive data and can be directly assigned. Destructuring assignment; setup must return an object. Once returned, the attribute can be used like vue2. The priority is the highest. Taking the following code as an example, it will be displayed: test from son's setup
In order to encapsulate reusability
A project or application cannot only talk about implementation, but also consider continuous integration and multiple In the past, the business complexity of the front-end was too low, so you used componentization or used global state management to solve the problem. It only took a little effort at most, but it is not feasible now. Calling the front-end and back-end interfaces is too wasteful of efficiency. , so it is very necessary to have a better architecture so that the front end can have complete encapsulation reusability support (that is, complete programming capabilities), so that it can hold the position of the view service in the cluster
[Related recommendations: "
vue.js tutorialThe above is the detailed content of Why vue uses setup. For more information, please follow other related articles on the PHP Chinese website!