您好,欢迎访问一九零五行业门户网

怎么在Vue3中使用<script lang=“ts“ setup>语法糖

迁移组件以下组件有两个道具(要显示的和一个标志)。通过另一个组件,计算模板中显示的小马图像的url,基于这两个道具。该组件还会在用户单击它时发出一个事件。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>
第一步,将属性添加到元素中。然后,我们只需要保留函数的内容:所有的样板都可以消失。您可以删除 和 中的函数: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>
隐式返回删除末尾的顶级绑定声明和导入语句会自动让它们在模板中变得使用可用。所以这里和可用,无需返回它们。when the pony image is clicked, the script will return.
这句话可以重写为:“我们可以仅通过导入组件,vue 就可以自动识别它在模板中的使用,因此可以省略声明。”。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>
我们差不多做到了:我们现在需要迁移 和 声明。propsemits
definepropsvue 提供了一个助手,你可以用它来定义你的道具。这是一个编译时助手(一个宏),因此您不必在代码中导入它。vue 在编译组件时会自动识别它。defineprops
defineprops返回道具:
const props = defineprops({ ponymodel: { type: object as proptype<ponymodel>, required: true }, isrunning: { type: boolean, default: false }});
defineprops将前一个声明作为参数接收。但是我们可以为typescript用户做得更好!props
defineprops是一般类型化的:你可以在没有参数的情况下调用它,但指定一个接口作为道具的“形状”。没有更可怕的写!我们可以使用正确的 typescript 类型,并添加以将 prop 标记为不需要 。用更通顺的语言改写为:object 作为 props 的类型时,是否需要指定具体的类型?
const props = defineprops<{ ponymodel: ponymodel; isrunning?: boolean;}>();
不过我们丢失了一些信息。在以前的版本中,我们可以指定其默认值为 .为了具有相同的行为,我们可以使用帮助程序:isrunningfalsewithdefaults
interface props { ponymodel: ponymodel; isrunning?: boolean;} const props = withdefaults(defineprops<props>(), { isrunning: false });
要迁移的最后一个剩余语法是声明。emits
defineemitsvue 提供了一个帮助程序,与帮助程序非常相似。这句话已经很清晰地表达了一个函数和其相应的操作,因此很难用单独一个句子来重写。但如果必须要重写,可以尝试以下几种方式:1. 这些函数用于定义和触发事件。2. defineemits, defineprops 和 defineemitsemit 函数都与事件有关。3. 如果你需要定义、设置和触发事件,可以使用 defineemits、defineprops 和 defineemitsemit 函数。4. 这几个函数可以帮助你在 vue.js 中管理事件
const emit = defineemits({ selected: () => true});
或者更好的是,使用typescript:
const emit = defineemits<{ (e: 'selected'): void;}>();
完整组件声明缩短了 10 行。这样减少30行组件来说还不错!这样做有助于提高可读性并更好地与 typescript 配合。虽然感觉让所有内容自动暴露到模板中有点奇怪,但由于没有换行符,您已经习惯了。
pony.vue
<template> <figure @click="clicked()"> <image :src="ponyimageurl" :alt="ponymodel.name" /> <figcaption>{{ ponymodel.name }}</figcaption> </figure></template> <script setup lang="ts">import { computed } from 'vue';import image from './image.vue';import { ponymodel } from '@/models/ponymodel'; interface props { ponymodel: ponymodel; isrunning?: boolean;} const props = withdefaults(defineprops<props>(), { isrunning: false }); const emit = defineemits<{ (e: 'selected'): void;}>(); const ponyimageurl = computed(() => `/pony-${props.ponymodel.color}${props.isrunning ? '-running' : ''}.gif`); function clicked() { emit('selected');}</script>
默认关闭和defineexpose有一些微妙的区别区分两种声明组件的方法:组件是“默认不启用的”。这意味着其他组件看不到组件内部定义的内容。script setup
举个例子,在下一章节中我们将看到组件能够访问另一个组件(通过使用 refs)。当函数被定义为 xx 时,所有函数返回的内容在父组件 yy 中也是可见的。如果 用 定义,则父组件不可见。 可以通过添加助手来选择暴露的内容。然后,公开的将作为 访问。ponyimageimagedefinecomponentsetupponyimagescript setupimagedefineexpose({ key: value })valuekey
以上就是怎么在vue3中使用<script lang=“ts“ setup>语法糖的详细内容。
其它类似信息

推荐信息