Summary of Vue3 setup

setup() is a new configuration item of Vue3, also known as combined API. The data methods used in components must be configured in setup, and its return value has two.

1. When an object is returned, the properties and methods in the object can be directly used in the template

 2. You can return a rendering function h, then you can directly customize the rendered content (simple understanding)

 3. Two parameters in setup

1. props, generally used to pass data from parent to child

 

 props is an object that contains the data we passed

2. context, the context object, contains some data processing methods of the component

They are attrs slots emit, which are the more important three, and the functions of these three are introduced below

attrs is equivalent to this.attrs in v2, its value is an object, as long as it is not received by props, it will be received by him,

slots refers to the content in the received slot, which is equivalent to this.$slots in v2

emit is generally used as a son-to-father, which is equivalent to the one in v2this.$emit

 

import son is for a more realistic simulation, and it doesn't make much sense

 In my last blog, when it comes to async functions, its return value is a promise object, and the return value is no longer a return object, so it cannot be an async function in setup.

And because setup is executed before beforecreate, there is no this in setup. Because of this, two lifecycle functions of beforecreate and created are integrated in setup, so there are no these two lifecycles in vue3

 

Guess you like

Origin blog.csdn.net/qq_45662523/article/details/126713933