初学vue3与ts:setup与setup()下的数据写法

把setup写在script里

<template><div><div class="index-title">script setup</div><div class="title">字符串:</div><div class="title-sub">ref版:{{strRef}}</div><div class="title-sub">ref版模板字符串:{{strRefTem}}</div><div class="title-sub">reactive版:{{strReactive.str}}</div><div class="title-sub">reactive版模板字符串:{{strReactiveTem.str}}</div><div class="title">数字:</div><div class="title-sub">ref版:{{numRef}}</div><div class="title-sub">reactive版:{{numReactive.num}}</div><div class="title">布尔:</div><div class="title-sub">ref版:{{booRef}}</div><div class="title-sub">reactive版:{{booReactive.boo}}</div><div class="title">数组:</div><div class="title-sub flex"><div>ref版:</div><div v-for="(item,index) in arrRef" :key="index">{{item}}</div></div><div class="title-sub flex"><div>reactive版:</div><div v-for="(item,index) in arrReactive.arr" :key="index">{{item}}</div></div><div class="title">用any定义一个定时器,10s后停止:</div><div class="title-sub">{{timeTam}}s</div><div class="title">对象:</div><div class="title-sub">ref版:name:{{objRef.name}},age:{{objRef.age}}</div><div class="title-sub">reactive版:name:{{objReactive.name}},age:{{objReactive.age}}</div></div>
</template><script setup lang="ts">import { ref, reactive } from 'vue' //引入,不要问为什么// 字符串// ref版 也可以用 ref<string>('我是ref版')const strRef = ref('我是ref版')// ref版-模板字符串const title = '你好,'const strRefTem = ref(`${title}我是模板字符串`)// reactive版const strReactive = reactive({str:'我是reactive版'})// reactive版-模板字符串const strReactiveTem = reactive({str:`${strRef.value}-模板字符串`})// 字符串重新赋值setTimeout(()=>{strRef.value = '我是ref版-2s后'strReactive.str = '我是reactive版-2s后'},2000)// 数字// ref版 也可以用 ref<number>(100)const numRef = ref(100)// reactive版const numReactive = reactive({num:100})// 布尔// ref版 也可以用 ref<boolean>(false)const booRef = ref(false)// reactive版const booReactive = reactive({boo:true})// 数组// ref版 也可以用 ref<object[]>([1,2,3,4])const arrRef = ref(['我','是','数','组'])// reactive版const arrReactive = reactive({arr:['上','面','说','的','没','错']})// 不限制类型const time = ref<any>(null)const timeTam = ref(0)time.value = setInterval(()=>{timeTam.value++;},1000)setTimeout(()=>{clearInterval(time.value)},10000)// 对象const objRef = ref({name:'林',age:18})// reactive版const objReactive = reactive({name:'吴',age:11})
</script><style>.index-title{font-size: 24px;font-weight: bold;}.title{font-weight: bold;font-size: 20px;padding-top: 20px;padding-left: 20px;}.title-sub{padding-left: 40px;margin-top: 10px;}.flex{display: flex;align-items: center;}
</style>

在script里用setup()

<template><div><div class="index-title">script setup()</div><div class="title">字符串:</div><div class="title-sub">ref版:{{strRef}}</div><div class="title-sub">ref版模板字符串:{{strRefTem}}</div><div class="title-sub">reactive版:{{strReactive.str}}</div><div class="title-sub">reactive版模板字符串:{{strReactiveTem.str}}</div><div class="title">数字:</div><div class="title-sub">ref版:{{numRef}}</div><div class="title-sub">reactive版:{{numReactive.num}}</div><div class="title">布尔:</div><div class="title-sub">ref版:{{booRef}}</div><div class="title-sub">reactive版:{{booReactive.boo}}</div><div class="title">数组:</div><div class="title-sub flex"><div>ref版:</div><div v-for="(item,index) in arrRef" :key="index">{{item}}</div></div><div class="title-sub flex"><div>reactive版:</div><div v-for="(item,index) in arrReactive.arr" :key="index">{{item}}</div></div><div class="title">用any定义一个定时器,10s后停止:</div><div class="title-sub">{{timeTam}}s</div><div class="title">对象:</div><div class="title-sub">ref版:name:{{objRef.name}},age:{{objRef.age}}</div><div class="title-sub">reactive版:name:{{objReactive.name}},age:{{objReactive.age}}</div></div>
</template><script lang="ts">import { defineComponent, ref, reactive } from 'vue' //引入,不要问为什么export default defineComponent({setup() {// 字符串// ref版 也可以用 ref<string>('我是ref版')const strRef = ref('我是ref版')// ref版-模板字符串const title = '你好,'const strRefTem = ref(`${title}我是模板字符串`)// reactive版const strReactive = reactive({str:'我是reactive版'})// reactive版-模板字符串const strReactiveTem = reactive({str:`${strRef.value}-模板字符串`})// 字符串重新赋值setTimeout(()=>{strRef.value = '我是ref版-2s后'strReactive.str = '我是reactive版-2s后'},2000)// 数字// ref版 也可以用 ref<number>(100)const numRef = ref(100)// reactive版const numReactive = reactive({num:100})// 布尔// ref版 也可以用 ref<boolean>(false)const booRef = ref(false)// reactive版const booReactive = reactive({boo:true})// 数组// ref版 也可以用 ref<object[]>([1,2,3,4])const arrRef = ref(['我','是','数','组'])// reactive版const arrReactive = reactive({arr:['上','面','说','的','没','错']})// 不限制类型const time = ref<any>(null)const timeTam = ref(0)time.value = setInterval(()=>{timeTam.value++;},1000)setTimeout(()=>{clearInterval(time.value)},10000)// 对象const objRef = ref({name:'林',age:18})// reactive版const objReactive = reactive({name:'吴',age:11})return{strRef,strRefTem,strReactive,strReactiveTem,numRef,numReactive,booRef,booReactive,arrRef,arrReactive,time,timeTam,objRef,objReactive}}})
</script><style>.index-title{font-size: 24px;font-weight: bold;}.title{font-weight: bold;font-size: 20px;padding-top: 20px;padding-left: 20px;}.title-sub{padding-left: 40px;margin-top: 10px;}.flex{display: flex;align-items: center;}
</style>

从上面的代码来看,setup比setup()方便的多,毕竟少了return,其他地方没什么改变,都一样
在这里插入图片描述

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

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

相关文章

arduino库之:sevseg库说明文档

该库将您的 Arduino 变成一个七段显示控制器。使用它可以轻松地在七段显示器上显示数字&#xff0c;无需任何额外的控制器。该库不支持移位寄存器。 支持&#xff1a; Arduino 连接 所有数字引脚都可以连接到Arduino的任何数字引脚&#xff0c;或具有数字支持的模拟引脚&#…

cocos游戏引擎制作的滚动框地图防止误点操作的简单方法

本篇文章主要讲解&#xff0c;使用cocos creator 来解决在我们日常滚动框开发中&#xff0c;滚动和触摸存在冲突的情况&#xff0c;导致的误触行为的解决办法。 日期&#xff1a;2023年11月25日 具体事项 说明&#xff1a;在我们滚动滚动框时&#xff0c;会出现误点的情况&…

Drools 7 JMX Mbean 及Metric 分析

Mbean mbean的打开很简单&#xff0c;使用jmx启动参数&#xff1a; -Dcom.sun.management.jmxremote.port9999 -Ddrools.mbeansenabled -Dcom.sun.management.jmxremote.authenticatefalse -Dcom.sun.management.jmxremote.sslfalse 但通过jconsole能直观看到的东西也很…

代码随想录-刷题第八天

344. 反转字符串 题目链接&#xff1a;344. 反转字符串 思路&#xff1a;让第一个和最后一个交换位置&#xff0c;第二个和倒数第二个交换位置&#xff0c;依次类推。 时间复杂度O(n)&#xff0c;空间复杂度O(1) class Solution {public void reverseString(char[] s) {// …

Vaex助力高效处理大规模数据集

大家好&#xff0c;在数据科学中&#xff0c;高效处理大规模数据集一直是个挑战。Vaex是一个功能强大的Python库&#xff0c;旨在通过提供快速且内存高效的数据操作和分析功能来解决这个问题。本文将探讨Vaex的实际应用&#xff0c;并展示如何简化工作流程。 1.惰性计算 Vaex…

利用 LD_PRELOAD 环境变量

文章目录 原理LD_PRELOAD介绍如何上传.so文件 例题 [虎符CTF 2022]ezphp 原理 LD_PRELOAD介绍 LD_PRELOAD是Linux系统的一个环境变量&#xff0c;它可以影响程序的运行时的链接&#xff08;Runtime linker&#xff09;&#xff0c;它允许你定义在程序运行前优先加载的动态链接…

【2023.11.26】Mybatis自定义映射规则学习

创建自定义映射规则 <select id"selectArtist" resultMap"test">select * from artist </select> 在SQL语句标签中将resultType修改为resultMap&#xff0c;即自定义映射的id。 编写自定义映射规则&#xff1a; <resultMap id"tes…

Linux - 系统调用(syscall)

说明 基于riscv64 soc linux_5.10.4平台&#xff0c;通过新增一个系统调用深入了解下系统调用实现原理。 简介 Linux 软件运行环境分为用户空间和内核空间&#xff0c;默认情况下&#xff0c;用户进程无法访问内核&#xff0c;既不能访问内核所在的内存空间&#xff0c;也不…

从范式标准谈一下OLTP和OLAP的区别

背景 在传统的OLAP和OLTP数据库的主要差别中&#xff0c;我们从数据组积的抽象层面看下两者的区别 范式上的区别 传统的OLTP数据库和OLAP数据库的在范式上重要的差异&#xff0c;传统的OLTP数据库是为进行事务处理服务的&#xff0c;其表结构遵循E-R关系模型&#xff0c;并且…

深入了解Java中SQL优化的关键技巧与实践

引言 介绍SQL优化对于Java应用性能的重要性&#xff0c;并概述本文将要讨论的内容。 1. 编写高效的SQL语句 - **索引的类型与使用&#xff1a;** 解释B-Tree索引、哈希索引等类型的区别&#xff0c;以及如何根据查询需求合理创建和使用索引。 - **查询优化器&#xff1a;** 说明…

【全栈开发】全栈开发框架/库

blitz https://github.com/blitz-js/blitzNext.js缺少的Fullstack工具包Blitz继承了Next.js的不足&#xff0c;为全球应用程序的交付和扩展提供了经过战斗测试的库和约定。 Redwoodjs https://github.com/redwoodjs/redwood初创企业应用程序框架Redwood是一个固执己见的、全…

python 点云las生成深度图

一、代码实现 import laspy import cv2 import numpy as np import matplotlib.pyplot as plt# 相机内参 CAM_WID, CAM_HGT = 475, 475 # 深度图尺寸 CAM_FX, CAM_FY = 5.2640790081811531e+02, 5.2616047137164196e+02 # fx/fy

DDD落地:从阿里单据系统,看DDD在大厂如何落地?

尼恩说在前面 在40岁老架构师 尼恩的读者交流群(50)中&#xff0c;最近有小伙伴拿到了一线互联网企业如阿里、滴滴、极兔、有赞、希音、百度、网易、美团的面试资格&#xff0c;遇到很多很重要的面试题&#xff1a; 谈谈你的DDD落地经验&#xff1f; 谈谈你对DDD的理解&#x…

职场份子钱随不随?这20个真相你需要知道!

职场份子钱随不随&#xff1f;这20个真相你需要知道&#xff01; 1.千万不要在老婆面前夸小姨子水灵。 2.盖世功劳&#xff0c;当不得一个矜字&#xff1b;弥天罪过&#xff0c;当不得一个悔字。 3.愚蠢的人永远只会根据答案判断难度。 4.改变自己的是神&#xff0c;企图改…

【数据库】表的连接在执行时的算法解析,嵌套循环连接算法的几种实现,多表连接中表的数量会影响什么

嵌套循环连接 ​专栏内容&#xff1a; 手写数据库toadb 本专栏主要介绍如何从零开发&#xff0c;开发的步骤&#xff0c;以及开发过程中的涉及的原理&#xff0c;遇到的问题等&#xff0c;让大家能跟上并且可以一起开发&#xff0c;让每个需要的人成为参与者。 本专栏会定期更新…

[论文阅读]CBAM——代码实现和讲解

CBAM 论文网址&#xff1a;CBAM 论文代码&#xff1a;CBAM 本文提出了一种卷积块注意力模块&#xff08;CBAM&#xff09;&#xff0c;它是卷积神经网络&#xff08;CNN&#xff09;的一种轻量级、高效的注意力模块。该模块沿着通道和空间两个独立维度依次推导注意力图&#x…

每日一题2023.11.26——打印沙漏【PTA】

题目要求&#xff1a; 本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”&#xff0c;要求按下列格式打印 ************ *****所谓“沙漏形状”&#xff0c;是指每行输出奇数个符号&#xff1b;各行符号中心对齐&#xff1b;相邻两行符号数差2&#xff1b;…

初级JVM

1、对象在哪块内存分配&#xff1f; 数组和对象在堆内存分配&#xff1b;某些对象没有逃逸出方法&#xff0c;可能被优化为在栈上分配 2、谈谈 JVM 中的常量池 JDK 1.8 开始 字符串常量池&#xff1a;存放在堆中&#xff0c;包括 String 对象执行 intern() 方法后存的地方、…

设计一个算法,将链表中所有结点的链接方向“原地”逆转,即要求仅利用原表的存储空间,换句话说,要求算法的空间复杂度为O(1)

设计一个算法&#xff0c;将链表中所有结点的链接方向“原地”逆转&#xff0c;即要求仅利用原表的存储空间&#xff0c;换句话说&#xff0c;要求算法的空间复杂度为O&#xff08;1&#xff09; 代码思路&#xff1a; 这里要求不用额外空间&#xff0c;那么就要考虑链表自身的…

Liunx系统使用超详细(一)

目录 一、Liunx系统的认识 二、Liunx和Windows区别 三、Liunx命令提示符介绍 四、Liunx目录结构 一、Liunx系统的认识 Linux系统是一种开源的、类Unix操作系统内核的实现&#xff0c;它基于Unix的设计原理和思想&#xff0c;并在全球范围内广泛应用。以下是对Linux系统的详…