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

Aug 09, 2022 am 10:04 AM
vue

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('setup.....')
		},
		beforeCreate() {
			console.log('beforeCreate...')
		},
	}
</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 './components/setupview'
export default {
  name: 'App',
  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('setup.....')
			//这种返回的数据不具有响应式
			// let name='tom'
			// return {
			// 	name,
			// }
			return {
				name:'tom',
				user:{
					username:'admin',
					password:'123'
				}
			}
		},
		beforeCreate() {
			// console.log('beforeCreate...')
		},
		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 './components/setupview'
export default {
  name: 'App',
  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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use echarts in vue How to use echarts in vue May 09, 2024 pm 04:24 PM

How to use echarts in vue

The role of export default in vue The role of export default in vue May 09, 2024 pm 06:48 PM

The role of export default in vue

How to use map function in vue How to use map function in vue May 09, 2024 pm 06:54 PM

How to use map function in vue

The difference between event and $event in vue The difference between event and $event in vue May 08, 2024 pm 04:42 PM

The difference between event and $event in vue

The difference between export and export default in vue The difference between export and export default in vue May 08, 2024 pm 05:27 PM

The difference between export and export default in vue

The role of onmounted in vue The role of onmounted in vue May 09, 2024 pm 02:51 PM

The role of onmounted in vue

Onmounted in vue corresponds to which life cycle of react Onmounted in vue corresponds to which life cycle of react May 09, 2024 pm 01:42 PM

Onmounted in vue corresponds to which life cycle of react

What are hooks in vue What are hooks in vue May 09, 2024 pm 06:33 PM

What are hooks in vue

See all articles