Home >
Web Front-end >
Vue.js >
A detailed explanation of script setup syntactic sugar in Vue3
A detailed explanation of script setup syntactic sugar in Vue3
青灯夜游
Release: 2022-11-22 20:07:20
forward
3596 people have browsed it
script setup syntax sugar
Combined API: setup()
Basic use
In the Composition API series of Vue 3, a new setup function is introduced, which is a component option and is executed before creating the component. , once the props are parsed and serve as the entry point to the composed API. [Learning video sharing: vue video tutorial, web front-end video]
setup option is a receiving props and context functions, we refer to the documentation for discussion. Additionally, we expose everything returned by setup to the rest of the component (computed properties, methods, lifecycle hooks, etc.) as well as the component's template.
The new setup option is executed before the component is created, after props is parsed, and is the entrance to the combined API.
Note: You should avoid using this in setup because it will not find the component instance. The call to setup occurs before the data property, computed property or methods are parsed, so they cannot> be used in setup was obtained.
In the script tag with setup added, we don’t have to declare and methods. This writing method will automatically expose all top-level variables and functions to the template (template ) Use here to emphasize the sentence " Exposed to the template is not the same thing as exposed to the outside "
TIP: said To put it more simply, when using the Vue 3 life cycle, the entire component-related business code can be executed in setup.
Because other life cycles will be enabled after setup, let’s compare the changes in the Vue3 life cycle of Vue2
Component life cycle
About the changes in the Vue life cycle, you can intuitively understand it from the following table:
Vue 2 life cycle
Vue 3 life Cycle
Execution time description
beforeCreate
setup
Execution before component creation
created
setup
Executed after the component is created
##beforeMount
onBeforeMount
Executed before the component is mounted on the node
mounted
onMounted
Executed after the component is mounted
beforeUpdate
onBeforeUpdate
Performed before component update
updated
onUpdated
Component update Execute after completion
beforeDestroy
onBeforeUnmount
Execute before component uninstallation
destroyed
onUnmounted
Executed after component unmounting is completed
errorCaptured
onErrorCaptured
Activated when catching an exception from a descendant component Hook function
The above is the detailed content of A detailed explanation of script setup syntactic sugar in Vue3. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn