SoFunction
Updated on 2025-02-28

vue3 setup syntax sugar component parameter transfer (definedProps, defineEmits, defineExpose) example detailed explanation of the example of define Props, defineEmits, defineExpose)

vue3 official document

  • definePropsanddefineEmitsAll can only be<script setup>Used inCompiler macros. They don't need to import, and they will follow<script setup>The processing process is compiled together.
  • definePropsReceive andpropsThe same value as the option,defineEmitsReceive andemitsThe same value as the option.

Father passed on to son - defineProps

Parent component

&lt;template&gt;
    &lt;div class="Father"&gt;
        &lt;p&gt;I'm the parent component&lt;/p&gt;
        &lt;!--  --&gt;
        &lt;son :ftext="ftext"&gt;&lt;/son&gt;
    &lt;/div&gt;
&lt;/template&gt;
&lt;script setup&gt;
import {ref} from 'vue'
import Son from './'
const ftext = ref('I am the parent component-text')
&lt;/script&gt;

Subcomponents

&lt;template&gt;
    &lt;div class="Son"&gt;
        &lt;p&gt;I'm a child component&lt;/p&gt;
       &lt;!-- Shows the value from the parent component --&gt;
       &lt;p&gt;Received value:{{ftext}}&lt;/p&gt;
    &lt;/div&gt;
&lt;/template&gt;
&lt;script setup&gt;
import {ref} from 'vue'
// setup syntax sugar writing method 
//defineProps to receive the component's pass valueconst props = defineProps({
    ftext: {
        type:String
    },
})
&lt;/script&gt;

Son passes to father - defineEmits

Subcomponents:

&lt;template&gt;
    &lt;div class="Son"&gt;
        &lt;p&gt;I'm a child component&lt;/p&gt;
        &lt;button @click="toValue"&gt;Click to pass the value to the parent component&lt;/button&gt;
    &lt;/div&gt;
&lt;/template&gt;
    
&lt;script setup&gt;
import {ref} from 'vue'
// setup syntax sugar writing method//Use defineEmits() to define the method to throw the child component, syntax defineEmits(['Method to throw'])const emit = defineEmits(['exposeData'])
 
const stext = ref('I am the value of the child component -ftext')
const toValue = ()=&gt;{
    emit('exposeData',stext)
}
    
&lt;/script&gt;

Parent component:

&lt;template&gt;
    &lt;div class="Father"&gt;
        &lt;p&gt;I'm the parent component&lt;/p&gt;
        &lt;!--  --&gt;
        &lt;son @exposeData="getData" :ftext="ftext"&gt;&lt;/son&gt;
    &lt;/div&gt;
&lt;/template&gt;
    
&lt;script setup&gt;
import {ref} from 'vue'
import Son from './'
const ftext = ref('I am the parent component-text')
const getData = (val)=&gt;{
    ("Receive the value of the child component",val)
}
&lt;/script&gt;

defineExpose 

Official explanation:

use<script setup>The component isClosed by default(i.e. referenced through template or$parentPublic instances of components obtained by the chain,Won'tExpose anything in<script setup>binding declared in  ).

Can be passeddefineExposeCompiler macros are explicitly specified in<script setup>Attributes to be exposed in the component

Subcomponents:

&lt;template&gt;
    &lt;div&gt;
        &lt;p&gt;I'm a child component&lt;/p&gt;
    &lt;/div&gt;
&lt;/template&gt;
    
&lt;script setup&gt;
import { ref } from 'vue';
 
    const stext = ref('I am the value of a child component')
    const sfunction = ()=&gt;{
        ("I'm a child component method")
    }
    defineExpose({
        stext,
        sfunction
    })
&lt;/script&gt;

Parent component:

&lt;template&gt;
	&lt;div class="todo-container"&gt;
		&lt;p&gt;I'm the parent component&lt;/p&gt;
		&lt;son ref="sonDom"&gt;&lt;/son&gt;
		&lt;button @click="getSonDom"&gt;Click&lt;/button&gt;
	&lt;/div&gt;
&lt;/template&gt;
 
&lt;script setup&gt;
import { ref ,nextTick} from 'vue';
	import son from './components/'
	const sonDom = ref(null) //Note that the naming here should be consistent with the naming above ref	const getSonDom = ()=&gt;{
		("sonDom",)
	}
 
	// You can't get the value of sonDom directly, and the child component node has not been generated yet	nextTick(()=&gt;{
		("sonDom",)
	})
&lt;/script&gt;

This is the article about the component transfer parameters of vue3-setup syntax sugar (definedProps, defineEmits, defineExpose). 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!