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

简记 Vue3(一)—— setup、ref、reactive、toRefs、toRef

个人简介

👀个人主页: 前端杂货铺
🙋‍♂️学习方向: 主攻前端方向,正逐渐往全干发展
📃个人状态: 研发工程师,现效力于中国工业软件事业
🚀人生格言: 积跬步至千里,积小流成江海
🥇推荐学习:🍍前端面试宝典 🎨100个小功能 🍉Vue2 🍋Vue3 🍓Vue2/3项目实战 🥝Node.js实战 🍒Three.js

🌕个人推广:每篇文章最下方都有加入方式,旨在交流学习&资源分享,快加入进来吧

文章目录

    • 前言
    • Ref 响应式(基本数据类型)
    • Reactive 响应式(对象类型)
    • ref 对比 reactive

前言

重拾 Vue3,查缺补漏、巩固基础。

Ref 响应式(基本数据类型)

使用 ref 包裹,即可实现基本数据类型的响应式。

<template><div class="person"><h2>{{ name }}</h2><h2>{{ age }}</h2><button @click="changeName">修改名字</button><button @click="changeAge">修改年龄</button><button @click="showTel">查看联系方式</button></div>
</template><script lang="ts" setup>
import { ref } from "vue";
let name = ref("zhangsan");
let age = ref(18);
let tel = 18588888888;function changeName() {name.value = "zs";
}function changeAge() {age.value += 1;
}function showTel() {alert(tel);
}
</script><style scoped>
.person {background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;
}
button {margin: 0 5px;
}
</style>

Reactive 响应式(对象类型)

使用 reactive 包裹,即可实现基本数据类型的响应式。

<template><div class="person"><h2>{{ car.brand }}: {{ car.price }}w</h2><button @click="changePrice">修改价格</button><ul v-for="item in person" :key="item.id"><li>{{ item.name }}</li></ul><button @click="changeFirstName">修改第一个人的名字</button></div>
</template><script lang="ts" setup>
import { reactive } from "vue";let car = reactive({ brand: "大奔", price: 80 });
let person = reactive([{ id: 1, name: "zhangsan" },{ id: 2, name: "lisi" },{ id: 3, name: "zahuopu" },
]);function changePrice() {car.price += 10;
}function changeFirstName() {person[0].name = "hh";
}
</script><style scoped>
.person {background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;
}
button {margin: 0 5px;
}
</style>

ref 对比 reactive

ref 创建的变量必须使用 .value,reactive 重新分配一个新对象,会失去响应式(可以通过 Object.assign 去替换整体)。

使用 reactive 实现对象响应式。

<template><div class="person"><h2>{{ car.brand }}: {{ car.price }}w</h2><button @click="changeCarInfo">修改汽车信息</button></div>
</template><script lang="ts" setup>
import { reactive } from "vue";let car = reactive({ brand: "大奔", price: 80 });function changeCarInfo() {Object.assign(car, { brand: "小米", price: 29.98 });
}
</script>

使用 ref 实现对象的响应式。

<template><div class="person"><h2>{{ car.brand }}: {{ car.price }}w</h2><button @click="changeCarInfo">修改汽车信息</button></div>
</template><script lang="ts" setup>
import { ref } from "vue";let car = ref({ brand: "大奔", price: 80 });function changeCarInfo() {car.value = { brand: "小米", price: 29.98 };
}
</script>

参考资料:
https://www.bilibili.com/video/BV1Za4y1r7KE?spm_id_from=333.788.player.switch&vd_source=f839085517d2b7548b2939bfe214d466&p=16


在这里插入图片描述



http://www.mrgr.cn/news/57231.html

相关文章:

  • 其他-自己手动更换汽车电磁进排气阀0.9.2
  • 入门介绍(一):脉冲神经网络(SNN)
  • MySQL-视图 (ಥ_ಥ)
  • Leetcode—194. 转置文件【中等】(Shell)
  • mysql表添加索引
  • Linux的环境变量不生效
  • Linux中如何理解一切皆文件
  • Python包——Matplotlib
  • 各种数据类型的定义与常规计算
  • 第23周Java主流框架入门-SpringMVC 3.拦截器
  • 【单元测试】深入解剖单元测试的思维逻辑
  • 【论文速看】DL最新进展20241023-多模态、无监督学习、多任务、图像修复
  • 【哈工大_操作系统实验】Lab8 终端设备的控制
  • 买华为系的车,这个理由无法拒绝
  • hass docker openwrt配置
  • 软件分享丨PDF Shaper
  • 不错的二次元个人导航页源码
  • 从新手到高手:map和set的使用技巧全攻略(C++)
  • 重学SpringBoot3-集成Hazelcast
  • 微服务-nacos
  • 掘金.计算位置 x 到 y 的最少步数(简单01)
  • 面试总结分享:25道数据库测试题
  • Vue01
  • leetcode hot100【LeetCode 49. 字母异位词分组】java实现
  • ScheduledThreadPoolExecutor的源码剖析
  • Visual Studio2022 Profile 工具使用