网站首页 > 博客文章 正文
简介
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
你 发表评论:
欢迎- 08-06nginx 反向代理
- 08-06跨表插入连续的日期,sheetsname函数#excel技巧
- 08-06初中生也能学的编程,不走弯路,先用后学
- 08-06find命令的“七种武器”:远不止-name和-type
- 08-06恶意代码常见的编程方式
- 08-06kali2021ping 外网不通
- 08-06因为一个函数strtok踩坑,我被老工程师无情嘲笑了
- 08-06hadoop集群搭建详细方法
- 38℃nginx 反向代理
- 最近发表
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- powershellfor (73)
- messagesource (71)
- plsql64位 (73)
- vueproxytable (64)
- npminstallsave (63)
- #NAME? (61)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- nacos启动失败 (64)
- ssh-add (70)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- qcombobox样式表 (68)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)