The following component has two props (to be displayed and a flag). Through another component, the URL of the pony image displayed in the template is calculated, based on these two props. The component also emits an event when the user clicks on it. The image selected while the Ponypony Model is running.
Pony.vue
<template> <figure @click="clicked()"> <Image :src="ponyImageUrl" :alt="ponyModel.name" /> <figcaption>{{ ponyModel.name }}</figcaption> </figure> </template> <script lang="ts"> import { computed, defineComponent, PropType } from 'vue'; import Image from './Image.vue'; import { PonyModel } from '@/models/PonyModel'; export default defineComponent({ components: { Image }, props: { ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } }, emits: { selected: () => true }, setup(props, { emit }) { const ponyImageUrl = computed(() => `/pony-${props.ponyModel.color}${props.isRunning ? '-running' : ''}.gif`); function clicked() { emit('selected'); } return { ponyImageUrl, clicked }; } }); </script>
The first step is to add attributes to the element. Then, we only need to keep the contents of the function: all the boilerplate can disappear. You can remove the functions in: setupscriptsetupdefineComponentsetupscript
Pony.vue
<script setup lang="ts"> import { computed, PropType } from 'vue'; import Image from './Image.vue'; import { PonyModel } from '@/models/PonyModel'; components: { Image }, props: { ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } }, emits: { selected: () => true }, const ponyImageUrl = computed(() => `/pony-${props.ponyModel.color}${props.isRunning ? '-running' : ''}.gif`); function clicked() { emit('selected'); } return { ponyImageUrl, clicked }; </script>
Remove the top-level binding declaration and import statement at the end will automatically make them available for use in the template. So here and available, no need to return them. When the pony image is clicked, the script will return.
This sentence can be rewritten as: "We can just import the component and Vue will automatically recognize its use in the template, so the declaration can be omitted ." componentsImagecomponents
Pony.vue
<script setup lang="ts"> import { computed, PropType } from 'vue'; import Image from './Image.vue'; import { PonyModel } from '@/models/PonyModel'; props: { ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } }, emits: { selected: () => true }, const ponyImageUrl = computed(() => `/pony-${props.ponyModel.color}${props.isRunning ? '-running' : ''}.gif`); function clicked() { emit('selected'); } </script>
We're almost there: we now need to migrate and declare. propsmits
Vue provides a helper you can use to define your props. This is a compile-time helper (a macro) so you don't have to import it in your code. Vue automatically recognizes your component when compiling it. defineProps
defineProps returns props:
const props = defineProps({ ponyModel: { type: Object as PropType<PonyModel>, required: true }, isRunning: { type: Boolean, default: false } });
defineProps receives the previous declaration as a parameter. But we can do better for TypeScript users! props
defineProps is generally typed: you can call it without arguments, but specify an interface as the "shape" of the prop. Nothing more terrible to write! We can use the correct TypeScript type and add ???? to mark the prop as unnecessary. Rewritten in more fluent language: "When Object is used as the type of Props, do I need to specify a specific type?"
const props = defineProps<{ ponyModel: PonyModel; isRunning?: boolean; }>();
But we lost some information. In previous versions, we could specify its default value as . To have the same behavior, we could use the helper: isRunningfalsewithDefaults
interface Props { ponyModel: PonyModel; isRunning?: boolean; } const props = withDefaults(defineProps<Props>(), { isRunning: false });
The last remaining syntax to migrate is declarations. emits
Vue provides a helper that is very similar to a helper. This sentence already clearly expresses a function and its corresponding operations, so it is difficult to rewrite it in a single sentence. But if you must rewrite, you can try the following methods: 1. These functions are used to define and trigger events. 2. The defineEmits, defineProps and defineEmitsemit functions are all related to events. 3. If you need to define, set and trigger events, you can use the defineEmits, defineProps and defineEmitsemit functions. 4. These functions can help you manage events in Vue.js
const emit = defineEmits({ selected: () => true });
Or better yet, use TypeScript:
const emit = defineEmits<{ (e: 'selected'): void; }>();
The full component declaration is 10 lines shorter. Not bad for reducing 30 lines of components! Doing this helps improve readability and works better with TypeScript. Although it feels a bit strange to have everything automatically exposed into the template, since there are no line breaks, you'll get used to it.
Pony.vue
There are some subtle differences that distinguish the two ways of declaring components: Components are "not enabled by default". This means that other components cannot see what is defined inside the component. script setup
For example, in the next chapter we will see that a component can access another component (by using refs). When a function is defined as XX, all content returned by the function is also visible in the parent component YY. If defined with , the parent component is not visible. Exposed content can be selected by adding helpers. The public ones will then be accessible as . PonyImageImagedefineComponentsetupPonyImagescript setupImagedefineExpose({ key: value })valuekey
The above is the detailed content of How to use