惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - springsnow

npoi读取word 内容控件 Vue3中 watch、watchEffect 详解 如何使用 Vue SFC Playground Vue3中如何响应式解构 props useTemplateRef使用 以后台方式启动RealVNC 在VS2022和VS2012共存的电脑上安装VS212扩展注意事项 VS中配置AnkhSVN源代码比较文件排列方式 使用VSCode撰写和发布博客园文章 DBever导入越南文Excel 复制对象中的一部分属性给另一个对象(对象部分属性解构到新对象) 使用metaWebBlog接口实现博客文章同步 .Net Core3.1上用EFCore的反向工程生成 水淼·文件批量处理器 如何高效的在博客园上编写MD格式的博客(插件pycnblog,推荐) 自动备份软件 —— Syncovery 7.98s Pro、Enterprise VMware 第三方百度网盘客户端 PanDownload、速盘、panlight 本地电脑视频播放器推荐PotPlayer、KMPlayer
toRefs学习
springsnow · 2024-09-26 · via 博客园 - springsnow

toRefs : 将一个响应式对象转换为一个普通对象,这个普通对象的每个属性都是指向源对象相应属性的 ref。每个单独的 ref 都是使用 <a href="https://link.zhihu.com/?target=https%3A//cn.vuejs.org/api/reactivity-utilities.html%23toref" class=" wrap external" target="_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">toRef()</a>创建的。通俗来说,就是复制 reactive 里的所有属性并转成 ref,改变某属性的值将更新对应ref的值,反之,改变复制后的ref的值,对应的reactive里的属性的值也会改变;

toRef : 基于响应式对象上的一个属性,创建一个对应的 ref。这样创建的 ref 与其源属性保持同步:改变源属性的值将更新 ref 的值,反之亦然。通俗来说,就是复制 reactive 里的单个属性并转成 ref,改变该属性的值将更新ref的值,反之,改变复制后的ref的值,对应的reactive里的属性的值也会改变,类似浅拷贝;

总结 :toRef复制的是reactive 里的单个属性,而toRefs复制的是reactive 里的所有属性。

1、toRefs 解构 ref对象

<template>
	<div>
		<p>{{ obj }}</p>
		<p>{{ a }}---{{ b }}</p>
		<button @click="changeobj">changeobj</button>
		<button @click="lookobj">lookobj</button>
	</div>
</template>

<script setup>
import { ref, toRefs } from "vue";

const obj = ref({ a: 10, b: 20 });
const { a, b } = toRefs(obj.value);

function changeobj() {
	a.value++;
	b.value++;
}

function lookobj() {
	console.log(a.value, obj.value.a);
}
</script>

2、toRefs 解构reactive

<template>
  name: {{ name }} <!-- 页面会更新 -->
  age: {{ age }}
</template>

<script setup lang="ts">
import { reactive, toRefs } from "vue";

let state = reactive({
  name: 'zly',
  age: 47
})

const { name, age } = toRefs(state);
//const {name,age} = state;


setInterval(() => {
  state.name += '--'
}, 1000)


</script>

3、toRef使用方式:

<template>
	<div>
		<p>{{ obj }}</p>
		<p>{{ b }}</p>
		<button @click="changeref">changeref</button>
		<button @click="changeobj">changeobj</button>
	</div>
</template>

<script setup>
import { reactive, toRef } from "vue";

const obj = reactive({
	a: 1,
	b: 2
})
// 复制 reactive 里的单个属性,转成 ref
const b = toRef(obj, 'b')

// 1、更改该 ref 会更新源属性
function changeref() {
	b.value++
}

console.log(obj.b) // 2
console.log(b.value) // 2


// 2、更改源属性也会更新该 ref
function changeobj() {
	obj.b++
}

console.log(obj.b) // 3
console.log(b.value) // 3
</script>