当前位置: 首页 > news >正文

04 在Vue3中使用setup语法糖

概述

Starting from Vue 3.0, Vue introduces a new syntactic sugar setup attribute for the <script> tag. This attribute allows you to write code using Composition API (which we will discuss further in Chapter 5, The Composition API) in SFCs and shorten the amount of code needed for writing simple components.

从 Vue 3.0 开始,Vue 为 <script> 标签引入了一个新的语法糖设置属性。该属性允许您在 SFC 中使用 Composition API编写代码,并缩短编写简单组件所需的代码量。

The code block residing within the <script setup> tag will then be compiled into a render() function before being deployed to the browser, providing better runtime performance.

<script setup> 标签中的代码块在部署到浏览器之前会被编译成 render() 函数,从而提供更好的运行时性能。

To start using this syntax, we take the following example code:

要开始使用这种语法,我们以下面的代码为例:

<script>
import logo from 'components/logo.vue'export default {components: {logo}
}
</script>

Then, we replace <script> with <script setup>, and remove all the code blocks of export default…. The example code now becomes as follows:

然后,将 <script> 替换为 <script setup>,并删除 export default… 的所有代码块。现在的示例代码如下

<script setup>
import logo from 'components/logo.vue'
</script>

In <template>, we use logo as usual:

<template> 中,我们照常使用徽标:

<template><header><a href="mywebsite.com"><logo /></a></header>
</template>

To define and use local data, instead of using data(), we can declare regular variables as local data and functions as local methods for that component directly. For example, to declare and render a local data property of color, we use the following code:

要定义和使用本地数据,我们可以不使用 data(),而是直接将常规变量声明为本地数据,将函数声明为该组件的本地方法。例如,要声明并呈现颜色的本地数据属性,我们可以使用以下代码:

<script setup>
const color = 'red';
</script>
<template><div>{{color}}</div>
</template>

The preceding code outputs the same result as the example in the previous section –red.

前面的代码输出的结果与上一节的示例相同–red。

As mentioned at the beginning of this section, <script setup> is the most useful when you need to use Composition API within SFCs. Still, we can always take advantage of its simplicity for simple components.

正如本节开头提到的,<script setup> 在需要在 SFC 中使用 Composition API 时最有用。不过,对于简单的组件,我们也可以利用它的简洁性。

NOTE: From this point onward, we will combine both approaches and use <script setup> whenever possible.

注意:从现在起,我们将把两种方法结合起来,尽可能使用 <script setup>

In the following exercise, we will go into more detail about how to use interpolation and data.

在下面的练习中,我们将详细介绍如何使用插值法和数据。

练习:条件插值

When you want to output data into your template or make elements on a page reactive, interpolate data into the template by using curly braces. Vue can understand and replace that placeholder with data.

当您想在模板中输出数据或使页面上的元素具有反应性时,可使用大括号在模板中插入数据。Vue 可以理解并用数据替换占位符。

Create a new Vue component file named Exercise1-02.vue in the src/components directory.

在 src/components 目录中新建一个名为 Exercise1-02.vue 的 Vue 组件文件。

Inside the Exercise1-02.vue component, let’s add data within the <script setup> tags by adding a function called data(), and return a key called title with your heading string as the value:

在 Exercise1-02.vue 组件中,让我们在 <script setup> 标记中添加数据,添加一个名为 data() 的函数,并返回一个名为 title 的键,其值为标题字符串:

<script>
export default {data() {return {title: 'My first component!',}},
}
</script>

Reference title by replacing your <h1> text with the interpolated value of {{ title }}:

<h1> 文本替换为 {{ title }} 的内插值,从而引用标题:

<template><div><h1>{{ title }}</h1></div>
</template>

修改App.vue,引入组件并渲染:

<script setup>
import Exercise102 from "./components/Exercise1-02.vue";
</script>
<template><Exercise102/>
</template>

When you save this document, the data title will now appear inside your h1 tag.

保存此文档时,数据标题将显示在 h1 标记内。

In Vue, interpolation will resolve any JavaScript that’s inside curly braces. For example, you can transform the text inside your curly braces using the toUpperCase() method:

在 Vue 中,插值将解决大括号内的任何 JavaScript 问题。例如,您可以使用 toUpperCase() 方法转换大括号内的文本:

<template><div><h1>{{ title.toUpperCase() }}</h1></div>
</template>

Interpolation can also handle conditional logic. Inside the data object, add a Boolean key-value pair, isUppercase: false:

插值还可以处理条件逻辑。在数据对象中,添加一个布尔键值对:isUppercase: false:

<script>
export default {data() {return {title: 'My first component!',isUppercase: false,}},
}
</script><template><div><h1>{{ isUppercase ? title.toUpperCase() : title }}</h1></div>
</template>

Now, let’s replace <script> with <script setup> and move all the local data declared within the data() function to its own variable names respectively, such as title and isUpperCase, as shown here:

现在,我们将 <script> 替换为 <script setup>,并将 data() 函数中声明的所有本地数据分别移到自己的变量名中,如 title 和 isUpperCase,如下所示:

<script setup>
const title ='My first component!';
const isUppercase = true;
</script>

In this exercise, we were able to apply inline conditions within the interpolated tags ({{}}) by using a Boolean variable. The feature allows us to modify what data to display without overly complicated situations, which can be helpful in certain use cases. We also learned how to write a more concise version of the component using <script setup> in the end.

在本练习中,我们可以使用布尔变量在插值标记({{}})中应用内联条件。这一功能让我们可以修改要显示的数据,而不必考虑过于复杂的情况,这在某些用例中很有帮助。最后,我们还学会了如何使用 <script setup> 编写更简洁的组件版本。

Since we are now familiar with using interpolation to bind local data, we will move on to our next topic – how to attach data and methods to HTML element events and attributes using Vue attributes.

既然我们现在已经熟悉了使用插值绑定本地数据,那么我们将进入下一个主题–如何使用 Vue 属性将数据和方法附加到 HTML 元素事件和属性上。

相关文章:

04 在Vue3中使用setup语法糖

概述 Starting from Vue 3.0, Vue introduces a new syntactic sugar setup attribute for the <script> tag. This attribute allows you to write code using Composition API (which we will discuss further in Chapter 5, The Composition API) in SFCs and shorte…...

vite+ts——user.ts——ts接口定义+axios请求的写法

import axios from axios; import qs from query-string; import {UserState} from /store/modules/user/types;export interface LoginData{username:string;password:string;grant_type?:string;scope?:string;client_id?:string;client_secret?:string;response_type?:…...

环境搭建及源码运行_java环境搭建_mysql安装

书到用时方恨少、觉知此时要躬行&#xff1b;拥有技术&#xff0c;成就未来&#xff0c;抖音视频教学地址&#xff1a;​​​​​​​​​​​​​​ 1、介绍 MySQL是一个关系型数据库管理系统&#xff0c;由瑞典MySQL AB 公司开发&#xff0c;属于 Oracle旗下产品。MySQL是最…...

Android camera的metadata

一、实现 先看一下metadata内部是什么样子&#xff1a; 可以看出&#xff0c;metadata 内部是一块连续的内存空间。 其内存分布大致可概括为&#xff1a; 区域一 &#xff1a;存 camera_metadata_t 结构体定义&#xff0c;占用内存 96 Byte 区域二 &#xff1a;保留区&#x…...

ElasticSearch面试题

1.介绍下es的架构&#xff1f; es采用的是分布式的架构&#xff0c;es集群中会有多个结点&#xff0c;而结点的角色主要有下面几种。 协调结点&#xff1a; 请求路由能力&#xff0c;将请求内容将请求转发给对应的结点进行处理。 master结点&#xff1a; 结点管理&#xff…...

C++ 数据结构知识点合集-C/C++ 数组允许定义可存储相同类型数据项的变量-供大家学习研究参考

#include <iostream> #include <cstring>using namespace std;// 声明一个结构体类型 Books struct Books {char title[50];char author[50];char subject[100];int book_id; };int main( ) {Books Book1; // 定义结构体类型 Books 的变量 Book1Books …...

【机器学习】5分钟掌握机器学习算法线上部署方法

5分钟掌握机器学习算法线上部署方法 1. 三种情况2. 如何转换PMML,并封装PMML2.1 什么是PMML2.2 PMML的使用方法范例3. 各个算法工具的工程实践4. 只用Linux的Shell来调度模型的实现方法5. 注意事项参考资料本文介绍业务模型的上线流程。首先在训练模型的工具上,一般三个模型训…...

Vue3-21-组件-子组件给父组件发送事件

情景描述 【子组件】中有一个按钮&#xff0c;点击按钮&#xff0c;触发一个事件&#xff0c; 我们希望这个事件的处理逻辑是&#xff0c;给【父组件】发送一条消息过去&#xff0c; 从而实现 【子组件】给【父组件】通信的效果。这个问题的解决就是 “发送事件” 这个操作。 …...

[密码学]AES

advanced encryption standard&#xff0c;又名rijndael密码&#xff0c;为两位比利时数学家的名字组合。 分组为128bit&#xff0c;密钥为128/192/256bit可选&#xff0c;对应加密轮数10/12/14轮。 基本操作为四种&#xff1a; 字节代换&#xff08;subBytes transformatio…...

CentOS 7 部署pure-ftp

文章目录 &#xff08;1&#xff09;简介&#xff08;2&#xff09;准备工作&#xff08;3&#xff09;更新系统&#xff08;4&#xff09;安装依赖环境&#xff08;5&#xff09;下载和解压pure-ftp源码包&#xff08;6&#xff09;编译和安装pure-ftp&#xff08;7&#xff0…...

Vue2-动态组件案例

1.component介绍 说明&#xff1a; Type: string | ComponentDefinition | ComponentConstructor Explanation: String: 如果你传递一个字符串给 is&#xff0c;它会被视为组件的名称&#xff0c;用于动态地渲染不同类型的组件。这是一个在运行时动态切换组件类型的常见用例。…...

【源码】车牌检测+QT界面+附带数据库

目录 1、基本介绍2、基本环境3、核心代码3.1、车牌识别3.2、车牌定位3.3、车牌坐标矫正 4、界面展示4.1、主界面4.2、车牌检测4.3、查询功能 5、演示6、链接 1、基本介绍 本项目采用tensorflow&#xff0c;opencv&#xff0c;pyside6和pymql编写&#xff0c;pyside6用来编写UI界…...

实战1-python爬取安全客新闻

一般步骤&#xff1a;确定网站--搭建关系--发送请求--接受响应--筛选数据--保存本地 1.拿到网站首先要查看我们要爬取的目录是否被允许 一般网站都会议/robots.txt目录&#xff0c;告诉你哪些地址可爬&#xff0c;哪些不可爬&#xff0c;以安全客为例子 2. 首先测试在不登录的…...

光栅化渲染:可见性问题和深度缓冲区算法

在前面第二章中&#xff0c;我们了解到&#xff0c;在投影点&#xff08;屏幕空间中的点&#xff09;的第三个坐标中&#xff0c;我们存储原始顶点 z 坐标&#xff08;相机空间中点的 z 坐标&#xff09;&#xff1a; 当一个像素与多个三角形重叠时&#xff0c;查找三角形表面上…...

docker入门小结

docker是什么&#xff1f;它有什么优势&#xff1f; 快速获取开箱即用的程序 docker使得所有的应用传输就像我们日常通过聊天工具文件传输一样&#xff0c;发送方将程序传输到超级码头而接收方也只需通过超级码头进行获取即可&#xff0c;就像一只鲸鱼拖着货物来回运输一样。…...

LLM Agent发展演进历史(观看metagpt视频笔记)

LLM相关的6篇重要的论文&#xff0c;其中4篇来自谷歌&#xff0c;2篇来自openai。技术路径演进大致是&#xff1a;SSL (Self-Supervised Learning) -> SFT (Supervised FineTune) IT (Instruction Tuning) -> RLHF。 word embedding的问题&#xff1a;新词如何处理&…...

Linux(操作系统)面经——part2

1、请你说说进程和线程的区别 1.进程是操作系统资源分配和调度的最小单位&#xff0c;实现操作系统内部的并发&#xff1b;线程是进程的子任务&#xff0c;cpu可以识别、执行的最小单位&#xff0c;实现程序内部的并发。 2.一个进程最少有一个线程或有多个&#xff0c;一个线程…...

Flink系列之:WITH clause

Flink系列之&#xff1a;WITH clause 适用流、批提供了一种编写辅助语句以在较大查询中使用的方法。这些语句通常称为公共表表达式 (CTE)&#xff0c;可以被视为定义仅针对一个查询而存在的临时视图。 WITH 语句的语法为&#xff1a; WITH <with_item_definition> [ , …...

JMeter直连数据库

JMeter直连数据库 使用场景操作步骤 使用场景 用作请求的参数化 登录时需要的用户名&#xff0c;密码可以从数据库中查询获取 用作结果的断言 添加购物车下订单&#xff0c;检查接口返回的订单号&#xff0c;是否与数据库中生成的订单号一致 清理垃圾数据 添加商品后&#xff…...

Linux部署MySQL5.7和8.0版本 | CentOS和Ubuntu系统详细步骤安装

一、MySQL数据库管理系统安装部署【简单】 简介 MySQL数据库管理系统&#xff08;后续简称MySQL&#xff09;&#xff0c;是一款知名的数据库系统&#xff0c;其特点是&#xff1a;轻量、简单、功能丰富。 MySQL数据库可谓是软件行业的明星产品&#xff0c;无论是后端开发、…...

铭豹扩展坞 USB转网口 突然无法识别解决方法

当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...

Appium+python自动化(十六)- ADB命令

简介 Android 调试桥(adb)是多种用途的工具&#xff0c;该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具&#xff0c;其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利&#xff0c;如安装和调试…...

Objective-C常用命名规范总结

【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名&#xff08;Class Name)2.协议名&#xff08;Protocol Name)3.方法名&#xff08;Method Name)4.属性名&#xff08;Property Name&#xff09;5.局部变量/实例变量&#xff08;Local / Instance Variables&…...

五年级数学知识边界总结思考-下册

目录 一、背景二、过程1.观察物体小学五年级下册“观察物体”知识点详解&#xff1a;由来、作用与意义**一、知识点核心内容****二、知识点的由来&#xff1a;从生活实践到数学抽象****三、知识的作用&#xff1a;解决实际问题的工具****四、学习的意义&#xff1a;培养核心素养…...

sqlserver 根据指定字符 解析拼接字符串

DECLARE LotNo NVARCHAR(50)A,B,C DECLARE xml XML ( SELECT <x> REPLACE(LotNo, ,, </x><x>) </x> ) DECLARE ErrorCode NVARCHAR(50) -- 提取 XML 中的值 SELECT value x.value(., VARCHAR(MAX))…...

基于Docker Compose部署Java微服务项目

一. 创建根项目 根项目&#xff08;父项目&#xff09;主要用于依赖管理 一些需要注意的点&#xff1a; 打包方式需要为 pom<modules>里需要注册子模块不要引入maven的打包插件&#xff0c;否则打包时会出问题 <?xml version"1.0" encoding"UTF-8…...

C# 类和继承(抽象类)

抽象类 抽象类是指设计为被继承的类。抽象类只能被用作其他类的基类。 不能创建抽象类的实例。抽象类使用abstract修饰符声明。 抽象类可以包含抽象成员或普通的非抽象成员。抽象类的成员可以是抽象成员和普通带 实现的成员的任意组合。抽象类自己可以派生自另一个抽象类。例…...

GitHub 趋势日报 (2025年06月08日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...

聊一聊接口测试的意义有哪些?

目录 一、隔离性 & 早期测试 二、保障系统集成质量 三、验证业务逻辑的核心层 四、提升测试效率与覆盖度 五、系统稳定性的守护者 六、驱动团队协作与契约管理 七、性能与扩展性的前置评估 八、持续交付的核心支撑 接口测试的意义可以从四个维度展开&#xff0c;首…...

【笔记】WSL 中 Rust 安装与测试完整记录

#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统&#xff1a;Ubuntu 24.04 LTS (WSL2)架构&#xff1a;x86_64 (GNU/Linux)Rust 版本&#xff1a;rustc 1.87.0 (2025-05-09)Cargo 版本&#xff1a;cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...