Home  >  Article  >  Web Front-end  >  How to use vue3 setup syntax sugar

How to use vue3 setup syntax sugar

王林
王林forward
2023-05-10 11:43:051390browse

    1. Introduction to setup syntax sugar

    You can use setup syntax sugar directly by adding the setup attribute in 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; properties and methods do not need to be returned and can be used directly in the template .

    	<template>
    		<my-component @click="func" :numb="numb"></my-component>
    	</template>
    	<script lang="ts" setup>
    		import {ref} from &#39;vue&#39;;
    		import myComponent from &#39;@/component/myComponent.vue&#39;;
    		//此时注册的变量或方法可以直接在template中使用而不需要导出
    		const numb = ref(0);
    		let func = ()=>{
    			numb.value++;
    		}
    	</script>

    2. New api in setup syntax sugar

    • defineProps: Child component receives props passed from parent component

    • defineEmits: The child component calls the method in the parent component

    • defineExpose: The child component exposes properties, 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 &#39;vue&#39;;
    		import myComponent from &#39;@/components/myComponent.vue&#39;;
    		const numb = ref(0);
    		let func = ()=>{
    			numb.value++;
    		}
    	</script>

    Child component code

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

    2.2defineEmits

    Subcomponent code

    	<template>
    		<div>{{numb}}</div>
    		<button @click="onClickButton">数值加1</button>
    	</template>
    	<script lang="ts" setup>
    		import {defineProps,defineEmits} from &#39;vue&#39;;
    		defineProps({
    			numb:{
    				type:Number,
    				default:NaN
    			}
    		})
    		const emit = defineEmits([&#39;addNumb&#39;]);
    		const onClickButton = ()=>{
    			//emit(父组件中的自定义方法,参数一,参数二,...)
    			emit("addNumb");
    		}
    	</script>

    Parent component code

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

    2.3defineExpose

    Subcomponent code

    	<template>
    		<div>子组件中的值{{numb}}</div>
    		<button @click="onClickButton">数值加1</button>
    	</template>
    	<script lang="ts" setup>
    		import {ref,defineExpose} from &#39;vue&#39;;
    		let numb = ref(0);
    		function onClickButton(){
    			numb.value++;	
    		}
    		//暴露出子组件中的属性
    		defineExpose({
    			numb 
    		})
    	</script>

    Parent component code

    	<template>
    		<my-comp ref="myComponent"></my-comp>
    		<button @click="onClickButton">获取子组件中暴露的值</button>
    	</template>
    	<script lang="ts" setup>
    		import {ref} from &#39;vue&#39;;
    		import myComp from &#39;@/components/myComponent.vue&#39;;
    		//注册ref,获取组件
    		const myComponent = ref();
    		function onClickButton(){
    			//在组件的value属性中获取暴露的值
    			console.log(myComponent.value.numb)  //0
    		}
    		//注意:在生命周期中使用或事件中使用都可以获取到值,
    		//但在setup中立即使用为undefined
    		console.log(myComponent.value.numb)  //undefined
    		const init = ()=>{
    			console.log(myComponent.value.numb)  //undefined
    		}
    		init()
    		onMounted(()=>{
    			console.log(myComponent.value.numb)  //0
    		})
    	</script>

    Supplement: used with ordinary script

    5101c0cdbdc49998c642c71f6b6410a8 can be used with ordinary3f1c4e4b6b16bbbd69b2ee476dc4f83a . Ordinary 3f1c4e4b6b16bbbd69b2ee476dc4f83a may be used in situations where these are needed.

    • Options that cannot be declared in 5101c0cdbdc49998c642c71f6b6410a8, such as inheritAttrs or custom options enabled through plugins

    • Declare named exports

    • Run side effects or create objects that only need to be executed once

    <script>
        // 普通 <script>, 在模块范围下执行(只执行一次)
        runSideEffectOnce()
        
        // 声明额外的选项
        export default {
          inheritAttrs: false,
          customOptions: {}
        }
    </script>
    
    <script setup>
        // 在 setup() 作用域中执行 (对每个实例皆如此)
    </script>

    The above is the detailed content of How to use vue3 setup syntax sugar. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete