网站首页 > 博客文章 正文
简介
toRef作用:将对象某一个属性,作为引用返回。可能说法比较绕口,我们通过实例来说明这个问题。
需求:我们实现触发事件,在只改动name的情况下,让product.name 和 name 的值都要从 电视机 改变成 空调,我们通过问题来引入toRef的应用场景
问题代码一
<template>
<h2>product.name:{{ product.name }}</h2>
<h2>name: {{ name }}</h2>
<button @click="hello">hello</button>
</template>
<script>
import {reactive} from 'vue';
export default {
name: 'App',
setup(){
let product = reactive({
name: '电视机',
sku: [{
count: 2,
title: '红色'
}]
});
let name = product.name;
return {
product,
name,
hello () {
name = '空调';
}
}
}
}
</script>
我们点击触发hello事件,页面上显示依旧是
点击之前 | 点击之后 |
product.name:电视机 | product.name:电视机 |
name:电视机 | name:电视机 |
问题代码二
我们给product.name 加上ref,让它变成响应式,我们改动如下代码
import {reactive, ref} from 'vue';
// setup...
let name = ref(product.name);
return {
product,
name,
hello () {
name.value = '空调';
}
}
可以看到如下表格,只有name发生改变
点击之前 | 点击之后 |
product.name:电视机 | product.name:电视机 |
name:电视机 | name:空调 |
toRef解决问题
import {reactive, toRef} from 'vue';
// setup...
let product = reactive({
name: '电视机',
sku: [{
count: 2,
title: '红色'
}]
});
let name = toRef(product, 'name');
return {
product,
name,
hello () {
name.value = '空调';
}
}
可以看到如下表格,toRef可以解决这个问题
点击之前 | 点击之后 |
product.name:电视机 | product.name:空调 |
name:电视机 | name:空调 |
toRefs使用
toRefs 返回对象中所有属性都响应式,相比之下比toRef写法跟简单,但是肯定会牺牲耗性能,代码如下:
<template>
<h2>product.name:{{ product.name }}</h2>
<h2>name:{{ name }}</h2>
<h2>price:{{ price }}</h2>
<button @click="hello">hello</button>
</template>
<script>
import {reactive, toRefs} from 'vue';
export default {
name: 'App',
setup(){
let product = reactive({
name: '电视机',
price: 2000,
sku: [{
count: 2,
title: '红色'
}],
});
let productObj = toRefs(product);
let name = productObj.name;
let price = productObj.price;
return {
product,
name,
price,
hello () {
name.value = '空调';
price.value = 3000;
}
}
}
}
</script>
猜你喜欢
- 2024-10-04 Vue3的使用toRef和toRefs(vue torefs)
- 2024-10-04 vue3的readonly、shallowReadonly、shallowReactive、shallowRef
- 2024-10-04 全网首发:Vue3.5 源码 useTemplateRef #vue
- 2024-10-04 一起学习vue3Ts-ref全家桶(vue3.0全家桶最全入门指南)
- 2024-10-04 Vue语法之ref reactive对比及使用场景
- 2024-10-04 吃瓜!前端人因 Vue3 的 Ref 语法糖提案打起来了
- 2024-10-04 终于搞懂了!原来vue3中template使用ref无需.value是因为这个
- 2024-10-04 vue通过ref实现子父通信(vue父子组件如何通信)
- 2024-10-04 [Vue 3] 为什么需要同时使用Ref和Reactive
- 2024-10-04 Vue 父子组件通信 ref 与 $parent / $children
你 发表评论:
欢迎- 最近发表
-
- 了解 PLC 编程中的浮点数(了解 plc 编程中的浮点数怎么算)
- 【Java教程】基础语法到高级特性(java高级用法)
- 如何使用 Fraction.js 解决 BigInt 的计算盲区?
- 三菱plc的数据类型(PLC的基础)(三菱plc数据指令)
- 计算机等级四级知识(计算机4级考试题目)
- 200Smart数据类型之浮点数(smart浮点数转换为整数)
- java使用Modbus4J读写Modbus RTU over Tcp示例
- 了解二进制编码:原码、反码、补码与移码
- 对比Cortex-M各处理器功能模块(cortex系列处理器分为哪3类,主要应用在哪些领域?)
- 通信小能手,Modbus TCP转Profibus DP网关硬控变送器与PLC通讯方案
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)