Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use defineProps and defineEmits in vue3.0 grammar sugar

Shulou Source: shulou.com Published: 2022-05-31 22:39:01 10月03日 Update

This article mainly introduces "how to use defineProps and defineEmits in vue3.0 grammatical sugar". In daily operation, I believe many people have doubts about how to use defineProps and defineEmits in vue3.0 grammatical sugar. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to use defineProps and defineEmits in vue3.0 grammatical sugar. Next, please follow the editor to study!

DefineProps, defineEmits1 and defineProps in grammatical sugar

Get component pass value

☆ ★ import {defineProps, defineEmits, computed, ref} from "vue" / / defineProps () / / adopt ts proprietary declaration There is a default value interface Props {value: number theme: string} const props = withDefaults (defineProps (), {value: 0, theme: 'blue'}) / / non-ts proprietary declaration / / let props = defineProps ({/ / value: Number,// theme: {/ / type: String / / default: 'blue'//} / /}) let width = ref (props.value) function mouseOver (I: number) {width.value = I} function mouseOut () {width.value = props.value} const fontWidth = computed (() = > `width: ${width.value} em `) const themeObj: {[key: string]: string} = {'black':' # 000, 'white':' # f80, 'red':' # f5222d, 'orange':' # 8f0, 'yellow':' # 80f, 'green':' # 0f6, 'blue':' # 08f,} const fontstyle = computed (() = > `color: ${themeObj [props.theme]};`). Rate {position: relative Display: inline-block;}. Rate > span.hollow {position: absolute; display: inline-block; top: 0; left: 0; width: 0; overflow: hidden;} 2, defineEmits

The child component passes to the parent component event

☆ ★ import {defineProps, defineEmits, computed, ref} from "vue" * * ditto code omit / / non-ts declaration / / let emits = defineEmits (['update-rate']) / / function onRate (num) {/ / emits (' update-rate', num) / /} / / adopt ts proprietary declaration Let emits = defineEmits () function onRate (num: number) {emits ('update-rate', num)} / / description of vue grammar sugar in the code above

Some tags can be omitted in the Vue2.X version.

Grammatical sugar refers to adding a method to achieve the same effect without affecting the function so as to facilitate program development.

Vue.js v-bind v-on instructions provide grammatical sugar, which can also be called abbreviations, such as v-bind can omit v-bind.

Just write a colon ":":

V-bind can be omitted as xx:xx, such as

Tags: Syntax learning components proprietary code methods more help utility next events colons functions situations instructions effects articles tags versions theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia Shulou Information vpn Apple