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 元素事件和属性上。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/227209.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

智能优化算法应用:基于动物迁徙算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于动物迁徙算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于动物迁徙算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.动物迁徙算法4.实验参数设定5.算法结果6.…

第十六章 : Spring Boot JWT 集成redis实现分布式token

第十六章 : Spring Boot JWT 集成redis实现分布式token 前言 本章知识重点:了解JWT 是什么,通过第一个JWT程序理解JWT的标头(Header)、有效载荷(Payload)和签名(Signature),通过Spring Boot JWT 集成redis的实践案例解读,灵活掌握JWT三大应用场景。 JWT是什么 J…

不同的葡萄酒瓶盖会影响葡萄酒饮用的体验

首先&#xff0c;不同的葡萄酒瓶盖会影响我们找到想要喝的葡萄酒的难易程度。螺旋盖、Zork瓶塞和起泡酒“蘑菇形瓶塞”赢得了直接的满足感&#xff0c;它们只需要拔瓶塞不需要开瓶器。来自云仓酒庄品牌雷盛红酒分享对于所有其他的酒瓶封口&#xff0c;我们都需要一个工具来打开…

论文阅读——Mask DINO(cvpr2023)

DINO是检测&#xff0c;Mask DINO是检测分割。 几个模型对比&#xff1a; 传统的检测分割中&#xff0c;检测头和分割头是平行的&#xff0c;Mask DINO使用二分图匹配bipartite matching提高匹配结果的准确性。 box对大的类别不计算损失&#xff0c;因为太大了&#xff0c;会…

Mac安装Typora实现markdown自由

一、什么是markdown Markdown 是一种轻量级标记语言&#xff0c;创始人为约翰格鲁伯&#xff08;John Gruber&#xff09;。 它允许人们使用易读易写的纯文本格式编写文档&#xff0c;然后转换成有效的 XHTML&#xff08;或者HTML&#xff09;文档。这种语言吸收了很多在电子邮…

verilog语法进阶-移位寄存器原语-单输入单输出

概述 verilog c代码 module primitive1(input clk , // system clock 50Mhz on boardinput rst_n, // system rst, low active input a , output y1, // output signaloutput y // output signal);SRLC16_1 #(.INIT(16h0000) // Initial Value of Shift Register ) SRLC16_1…

Python学习笔记第七十六天(OpenCV视频应用)

Python学习笔记第七十六天 OpenCV视频应用从相机中读取视频从文件中播放视频保存视频 后记 OpenCV视频应用 从相机中读取视频 OpenCV提供了cv2.VideoCapture()函数&#xff0c;用于从相机中读取视频。这个函数接受一个参数&#xff0c;即摄像头设备的编号。默认情况下&#x…

SpringCloud-高级篇(八)

&#xff08;1&#xff09;TCC模式 前面学了XA和AT模式&#xff0c;这两种模式最终都能实现一致性&#xff0c;和隔离性&#xff0c;XA是强一致&#xff0c;AT是最终一致&#xff0c;隔离性呢XA是在第一阶段不提交&#xff0c;基于事务本身的特性来完成隔离&#xff0c;AT则是…

uniGUI学习之UniTreeview

UniTreeview中能改变一级目录的字体和颜色 function beforeInit(sender, config) { ID"#"config.id; Ext.util.CSS.createStyleSheet( ${ID} .x-tree-node-text{color:green;font-weight:800;} ${ID} .x-tree-elbow-line ~ span{color:black;font-weight:400;} ); }

Kubernetes Pod 网段与主机内网网段互通

开发环境的需求 开发环境部署 K8s 后&#xff0c;服务器会部署在 K8s 里&#xff0c;通常 Pod 网段被隔离&#xff0c;主机无法访问 实际开发需求&#xff0c;往往需要当前开发调试的服务主机本地部署&#xff0c;其他服则在 K8s 内 因此&#xff0c;使用 K8s &#xff0c;必…

uniGUI学习之UniHTMLMemo1富文本编辑器

1]系统自带的富文本编辑器 2]jQueryBootstarp富文本编辑器插件summernote.js 1]系统自带的富文本编辑器 1、末尾增加<p> 2、增加字体 3、解决滚屏问题 4、输入长度限制问题 5、显示 并 编辑 HTML源代码(主要是图片处理) 1、末尾增加<p> UniHTMLMemo1.Lines…

【MySQL】启动 和 连接 MySQL

启动停止 mysql安装成功后在cmd输入 net start mysql80 //启动 net stop mysql80 //停止 mysql连接 方式1. 通过客户端去连接 方式2.使用cmd去连接 描述&#xff1a;-u是指定 用户 -p是指定密码 mysql -u root -p password

NVM下载和安装NodeJS教程(环境变量配置)

前言:一个公司很多项目,可能每个项目node版本要求不一样,导致每次切换项目,你要重新下载node,非常麻烦,这个时候,就有了nvm,非常的方便实用,也是程序员必备. 1.nvm是什么? nvm全英文也叫node.js version management&#xff0c;是一个nodejs的版本管理工具。nvm和n都是node.…

git各分支简介

一、master 主分支 所有用户可见的正式版本&#xff0c;都从master发布&#xff08;也是用于部署生产环境的分支&#xff0c;确保master分支稳定性&#xff09; 主分支作为稳定的唯一代码库&#xff0c;不做任何开发使用 master 分支一般由develop以及hotfix分支合并&#…

基于java+swing+mysq学生成绩管理系统(含课程报告)

基于javaswingmysq学生成绩管理系统_含课程报告 一、系统介绍二、功能展示三、项目相关3.1 乱码问题3.2 如何将GBK编码系统修改为UTF-8编码的系统&#xff1f; 四、其它1.其他系统实现 五、源码下载 一、系统介绍 本系统使用 Swing MySQL IntelliJ IDEA 开发。为管理人员提供…

生产环境_Spark解析JSON字符串并插入到MySQL数据库

业务背景&#xff1a; 最近开发有一个需求&#xff0c;是这样的 我需要将一段从前端传过来的JSON字符串进行解析&#xff0c;并从中提取出所需的数据&#xff0c;然后将这些数据插入到MySQL数据库中。 json格式样例如下 { \"区域编号\": \"001\", …

什么是npm?

npm&#xff08;全称Node Package Manager&#xff09;是一个用于管理Node.js包和模块的命令行工具。它是一个包管理器&#xff0c;允许您在您的项目中方便地安装、管理和升级模块和依赖项。 npm已成为JavaScript生态系统中最流行的包管理器之一&#xff0c;它提供了一个庞大的…

不设默认值(js的问题)

给动态变量设置默认值是一个非常好的实践&#xff0c;可以预防发生意想不到的错误。下面是一个常见错误的例子&#xff1a; function addTwoNumbers(a, b) { ​​ console.log(a b); ​​} ​​addTwoNumbers(); ​​// NaN 结果是 "NaN"&#xff0c;因为 a 是…

Mac brew install youtube-dl 【 youtube 下载工具:youtube-dl 安装】

文章目录 1. 简介2. 预备3. 安装4. 命令5. 测试 1. 简介 youtube-dl - 从youtube.com或其他视频平台下载视频 https://github.com/ytdl-org/youtube-dl 2. 预备 安装并配置 git安装 brew 3. 安装 MacBook-Pro ~ % brew install youtube-dl Warning: youtube-dl has been …

如何在PHP中使用PDO预处理语句?

在PHP中使用PDO&#xff08;PHP Data Objects&#xff09;来执行预处理语句是一种安全的方式来与数据库进行交互&#xff0c;以防止SQL注入攻击。以下是如何在PHP中使用PDO预处理语句的基本步骤&#xff1a; 建立数据库连接&#xff1a; 首先&#xff0c;您需要使用PDO建立与数…