SoFunction
Updated on 2025-04-05

vue3:setup syntax sugar usage tutorial

Syntax Sugar Introduction

You can use the setup syntax sugar directly by adding the setup attribute to the script tag.

After using setup syntax sugar,There is no need to write a setup function; components only need to be introduced without registration; attributes and methods do not need to be returned, and can be used directly in the template template.

	<template>
		<my-component @click="func" :numb="numb"></my-component>
	</template>
	<script lang="ts" setup>
		import {ref} from 'vue';
		import myComponent from '@/component/';
		//The registered variables or methods can be used directly in the template without exporting		const numb = ref(0);
		let func = ()=>{
			++;
		}
	</script>

New API in syntax sugar

  • defineProps: The child component receives props from the parent component
  • defineEmits: The child component calls a method in the parent component
  • defineExpose: The child component exposes the attributes, which can be obtained in the parent component

2.1defineProps

Parent component code

	<template>
		<my-component @click="func" :numb="numb"></my-component>
	</template>
	<script lang="ts" setup>
		import {ref} from 'vue';
		import myComponent from '@/components/';
		const numb = ref(0);
		let func = ()=>{
			++;
		}
	</script>

Subcomponent code

	<template>
		<div>{{numb}}</div>
	</template>
	<script lang="ts" setup>
		import {defineProps} from 'vue';
		defineProps({
			numb:{
				type:Number,
				default:NaN
			}
		})
	</script>

2.2defineEmits

Subcomponent code

	&lt;template&gt;
		&lt;div&gt;{{numb}}&lt;/div&gt;
		&lt;button @click="onClickButton"&gt;Value addition1&lt;/button&gt;
	&lt;/template&gt;
	&lt;script lang="ts" setup&gt;
		import {defineProps,defineEmits} from 'vue';
		defineProps({
			numb:{
				type:Number,
				default:NaN
			}
		})
		const emit = defineEmits(['addNumb']);
		const onClickButton = ()=&gt;{
			//emit (custom methods in parent component, parameter one, parameter two,...)			emit("addNumb");
		}
	&lt;/script&gt;

Parent component code

	<template>
		<my-component @addNumb="func" :numb="numb"></my-component>
	</template>
	<script lang="ts" setup>
		import {ref} from 'vue';
		import myComponent from '@/components/';
		const numb = ref(0);
		let func = ()=>{
			++;
		}
	</script>

2.3defineExpose

Subcomponent code

	&lt;template&gt;
		&lt;div&gt;Values ​​in subcomponents{{numb}}&lt;/div&gt;
		&lt;button @click="onClickButton"&gt;Value addition1&lt;/button&gt;
	&lt;/template&gt;
	&lt;script lang="ts" setup&gt;
		import {ref,defineExpose} from 'vue';
		let numb = ref(0);
		function onClickButton(){
			++;	
		}
		//Expose properties in subcomponents		defineExpose({
			numb 
		})
	&lt;/script&gt;

Parent component code

	&lt;template&gt;
		&lt;my-comp ref="myComponent"&gt;&lt;/my-comp&gt;
		&lt;button @click="onClickButton"&gt;Get the exposed values ​​in the child component&lt;/button&gt;
	&lt;/template&gt;
	&lt;script lang="ts" setup&gt;
		import {ref} from 'vue';
		import myComp from '@/components/';
		//Register ref and get component		const myComponent = ref();
		function onClickButton(){
			//Get the exposed value in the component's value property			()  //0
		}
		//Note: You can get the value when used during the life cycle or during events.		//But use immediately in setup as undefined		()  //undefined
		const init = ()=&gt;{
			()  //undefined
		}
		init()
		onMounted(()=&gt;{
			()  //0
		})
	&lt;/script&gt;

Added: Used with regular script

<script setup> can be used with normal <script>. Ordinary <script> may be used in the event of these needs.

  • Options that cannot be declared in <script setup>, such as inheritAttrs or custom options enabled via plugin
  • Declaration name export
  • Run side effects or create objects that only need to be executed once
&lt;script&gt;
    // Normal <script>, executed in the module scope (execute only once)    runSideEffectOnce()
    
    // Declare additional options    export default {
      inheritAttrs: false,
      customOptions: {}
    }
&lt;/script&gt;

&lt;script setup&gt;
    // Execute in setup() scope (for every instance)&lt;/script&gt;

Summarize

This is the end of this article about the use of vue3:setup syntax sugar. For more related contents of vue3:setup syntax sugar, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!