怎么在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
defineProps
Vue 提供了一个助手,你可以用它来定义你的道具。这是一个编译时助手(一个宏),因此您不必在代码中导入它。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
defineEmits
Vue 提供了一个帮助程序,与帮助程序非常相似。这句话已经很清晰地表达了一个函数和其相应的操作,因此很难用单独一个句子来重写。但如果必须要重写,可以尝试以下几种方式: 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
{{ ponyModel.name }}
默认关闭和defineExpose
有一些微妙的区别区分两种声明组件的方法:组件是“默认不启用的”。这意味着其他组件看不到组件内部定义的内容。script setup
举个例子,在下一章节中我们将看到组件能够访问另一个组件(通过使用 refs)。当函数被定义为 XX 时,所有函数返回的内容在父组件 YY 中也是可见的。如果 用 定义,则父组件不可见。 可以通过添加助手来选择暴露的内容。然后,公开的将作为 访问。PonyImageImagedefineComponentsetupPonyImagescript setupImagedefineExpose({ key: value })valuekey
以上是怎么在Vue3中使用<script lang=“ts“ setup>语法糖的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

tinymce是一个功能齐全的富文本编辑器插件,但在vue中引入tinymce并不像别的Vue富文本插件一样那么顺利,tinymce本身并不适配Vue,还需要引入@tinymce/tinymce-vue,并且它是国外的富文本插件,没有通过中文版本,需要在其官网下载翻译包(可能需要翻墙)。1、安装相关依赖npminstalltinymce-Snpminstall@tinymce/tinymce-vue-S2、下载中文包3.引入皮肤和汉化包在项目public文件夹下新建tinymce文件夹,将下载的

vue3+vite:src使用require动态导入图片报错和解决方法vue3+vite动态的导入多张图片vue3如果使用的是typescript开发,就会出现require引入图片报错,requireisnotdefined不能像使用vue2这样imgUrl:require(’…/assets/test.png’)导入,是因为typescript不支持require所以用import导入,下面介绍如何解决:使用awaitimport

想要实现页面的局部刷新,我们只需要实现局部组件(dom)的重新渲染。在Vue中,想要实现这一效果最简便的方式方法就是使用v-if指令。在Vue2中我们除了使用v-if指令让局部dom的重新渲染,也可以新建一个空白组件,需要刷新局部页面时跳转至这个空白组件页面,然后在空白组件内的beforeRouteEnter守卫中又跳转回原来的页面。如下图所示,如何在Vue3.X中实现点击刷新按钮实现红框范围内的dom重新加载,并展示对应的加载状态。由于Vue3.X中scriptsetup语法中组件内守卫只有o

Vue实现博客前端,需要实现markdown的解析,如果有代码则需要实现代码的高亮。Vue的markdown解析库有很多,如markdown-it、vue-markdown-loader、marked、vue-markdown等。这些库都大同小异。这里选用的是marked,代码高亮的库选用的是highlight.js。具体实现步骤如下:一、安装依赖库在vue项目下打开命令窗口,并输入以下命令npminstallmarked-save//marked用于将markdown转换成htmlnpmins

vue3+ts+axios+pinia实现无感刷新1.先在项目中下载aiXos和pinianpmipinia--savenpminstallaxios--save2.封装axios请求-----下载js-cookienpmiJS-cookie-s//引入aixosimporttype{AxiosRequestConfig,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess

前言无论是vue还是react,当遇到多处重复代码的时候,我们都会想着如何复用这些代码,而不是一个文件里充斥着一堆冗余代码。实际上,vue和react都可以通过抽组件的方式来达到复用,但如果遇到一些很小的代码片段,你又不想抽到另外一个文件的情况下,相比而言,react可以在相同文件里面声明对应的小组件,或者通过renderfunction来实现,如:constDemo:FC=({msg})=>{returndemomsgis{msg}}constApp:FC=()=>{return(

vue3项目打包发布到服务器后访问页面显示空白1、处理vue.config.js文件中的publicPath处理如下:const{defineConfig}=require('@vue/cli-service')module.exports=defineConfig({publicPath:process.env.NODE_ENV==='production'?'./':'/&

最终效果安装VueCropper组件yarnaddvue-cropper@next上面的安装值针对Vue3的,如果时Vue2或者想使用其他的方式引用,请访问它的npm官方地址:官方教程。在组件中引用使用时也很简单,只需要引入对应的组件和它的样式文件,我这里没有在全局引用,只在我的组件文件中引入import{userInfoByRequest}from'../js/api'import{VueCropper}from'vue-cropper&
