Home > Web Front-end > Vue.js > A brief analysis of the setup function (entry point) of Vue3

A brief analysis of the setup function (entry point) of Vue3

藏色散人
Release: 2022-08-09 10:04:31
forward
2142 people have browsed it

Composition Api
The setup function is a new component option. Serves as the entry point for using the Composition API within a component.
Calling timing:
The setup function will be called before the beforeCreate hook
Return value
If setup returns an object, the properties of the object can be accessed in the component template
Parameters
Receive two parameters

setup.vue

<template>
	<div>
		setup
	</div>
</template>
 
<script>
	export default{
		setup(){
			console.log(&#39;setup.....&#39;)
		},
		beforeCreate() {
			console.log(&#39;beforeCreate...&#39;)
		},
	}
</script>
 
<style>
</style>
Copy after login

app.vue

 <template>
	<comp-setup>
		
	</comp-setup>
</template>
 
<script>
/*eslint no-mixed-spaces-and-tabs: ["error", "smart-tabs"]*/
import CompSetup from &#39;./components/setupview&#39;
export default {
  name: &#39;App&#39;,
  components: {
	  CompSetup,
  }
}
</script>
 
<style>
 
</style>
Copy after login

##Receive parameters:

setup.vue

<template>
	<div>
		{{ name }}
		<p>{{ user.username }}</p>
	</div>
</template>
 
<script>
	export default{
		//setup不能访问this
		//可以接收参数
		setup(props,context){
			// console.log(&#39;setup.....&#39;)
			//这种返回的数据不具有响应式
			// let name=&#39;tom&#39;
			// return {
			// 	name,
			// }
			return {
				name:&#39;tom&#39;,
				user:{
					username:&#39;admin&#39;,
					password:&#39;123&#39;
				}
			}
		},
		beforeCreate() {
			// console.log(&#39;beforeCreate...&#39;)
		},
		props:{
			msg:String
		}
	}
</script>
 
<style>
</style>
Copy after login

app.vue

<template>
	<comp-setup msg="welcome">
		
	</comp-setup>
</template>

<script>
/*eslint no-mixed-spaces-and-tabs: ["error", "smart-tabs"]*/
import CompSetup from &#39;./components/setupview&#39;
export default {
  name: &#39;App&#39;,
  components: {
	  CompSetup,
  }
}
</script>

<style>

</style>
Copy after login
[Related recommendations:

vue.js video tutorial]

The above is the detailed content of A brief analysis of the setup function (entry point) of Vue3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:csdn.net
Statement of this 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template