Vue3初探之ref、reactive以及改变数组的值

作者:风随心飞飞 时间:2024-04-27 16:06:00 

概况

Vue3 里要实现数据的响应式监听一共有两种方式既:ref 和 reactive

他们既有区别又有联系。

ref()

ref数据响应式监听。ref 函数传入一个值作为参数,一般传入基本数据类型,返回一个基于该值的响应式Ref对象,该对象中的值一旦被改变和访问,都会被跟踪到,就像我们改写后的示例代码一样,通过修改 count.value 的值,可以触发模板的重新渲染,显示最新的值

reactive()

reactive

reactive 是 Vue3 中提供的实现响应式数据的⽅法。

在 Vue2 中响应式数据是通过 defineProperty 来实现的,在 Vue3 中响应式数据是通过 ES6 的 Proxy 来实现的。具体参照,。

reactive 参数必须是对象 (json / arr)

本质: 就是将传⼊的数据包装成⼀个Proxy对象

如果给 reactive 传递了其它对象(如Date对象)

默认情况下,修改对象⽆法实现界⾯的数据绑定更新。

如果需要更新,需要进⾏重新赋值。(即不允许直接操作数据,需要放个新的数据来替代原数据)

在 reactive 使⽤基本类型参数

基本类型(数字、字符串、布尔值)在 reactive 中⽆法被创建成 proxy 对象,也就⽆法实现监听。⽆法实现响应式

<template>
<div>
<p>{{msg}}</p>
<button @click="c">button</button>
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
let msg = reactive(0)
function c() {
console.log(msg);
msg ++;
}
return {
msg,
c
};
}
}
</script>

点击 button ,我们期望的结果是数字从 0 变成 1,然⽽实际上界⾯上的数字并没有发⽣任何改变。

查看控制台,它的输出是这样的(我点了 3 次)

出现提⽰

value cannot be made reactive: 0

⽽输出的值确实发⽣了变化,只不过这种变化并没有反馈到界⾯上,也就是说并没有实现双向数据绑定。当然,如果是 ref 的话,就不存在
这样的问题。⽽如果要使⽤ reactive ,我们需要将参数从 基本类型 转化为 对象。

<template>
<div>
<p>{{msg.num}}</p>
<button @click="c">button</button>
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
let msg = reactive({
num: 0
})
function c() {
console.log(msg);
msg.num ++;
}
return {
msg,
c
};
}
}
</script>

将参数替换成了对象 {num: 0},此时,点击按钮界⾯就会产⽣改变(我点了 3 次)。

在控制台打印消息

可以看到,msg 成功被创建成了 proxy 对象,他通过劫持对象的 get 和 set ⽅法实现了对象的双向数据绑定。

深层的、对象内部的变化也能被察觉到(注意下⾯代码中的 inner )

<template>
<div>
<p>{{msg.num.inner}}</p>
<button @click="c">button</button>
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
let msg = reactive({
num: {
inner: 0
}
})
function c() {
console.log(msg);
msg.num.inner ++;
}
return {
msg,
c
};
}
}
</script>

数组变化也不在话下。

<template>
<div>
<p>{{msg}}</p>
<button @click="c">button</button>
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
let msg = reactive([1, 2, 3])
function c() {
console.log(msg);
msg[0] += 1;
msg[1] = 5;
}
return {
msg,
c
};
}
}
</script>

在 reactive 使⽤ Date 参数

如果参数不是数组、对象,⽽是稍微奇怪⼀点的数据类型,例如说 Date ,那么⿇烦⼜来了。

<template>
<div>
<p>{{msg}}</p>
<button @click="c">button</button>
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
let msg = reactive(new Date())
function c() {
console.log(msg);
msg.setDate(msg.getDate() + 1);
console.log(msg);
}
return {
msg,
c
};
}
}
</script>

这⾥我先打印了 msg 两次,可以看到,点击⼀次 button ,msg 的数据是存在变化的,但界⾯并未发⽣变化,同时我们发现在控制台
⾥,msg 并未被识别成 proxy。

就算我们把 Date 放在对象⾥,就像这样

<template>
<div>
<p>{{msg.date}}</p>
<button @click="c">button</button>
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
let msg = reactive({
date: new Date()
});
function c() {
console.log(msg);
msg.date.setDate(msg.date.getDate() + 1);
console.log(msg);
}
return {
msg,
c
};
}
}
</script>

也仍然不起效果。

显然,对于这种数据类型,我们需要做特殊处理。

这个特殊处理就是重新赋值(,⽽不是直接修改原来的值)。

<template>
<div>
<p>{{msg.date}}</p>
<button @click="c">button</button>
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
let msg = reactive({
date: new Date()
});
function c() {
console.log(msg);
msg.date.setDate((msg.date.getDate() + 1));
msg.date = new Date(msg.date);
console.log(msg);
}
return {
msg,
c
};
}
}
</script>

这⾥我采⽤了拷贝的⽅案重新赋值了 msg.date,界⾯成功发⽣了变化(⽇期 + 1)。

vue3中改变数组的值

let arr = reactive([{id:1},{id:2}])

不可行:arr=[], arr = reactive([])

可行:arr.splice(0,arr.length)
arr.length = 0

原因:前者创建了新对象,页面渲染的原对象还是没有改变;后者改变的是原页面对象

来源:https://blog.csdn.net/u014212540/article/details/124280339

标签:ref,reactive,数组
0
投稿

猜你喜欢

  • Pandas —— resample()重采样和asfreq()频度转换方式

    2023-12-10 16:51:09
  • 如何在Access 2007数据库中添加附件

    2008-11-21 12:32:00
  • 使用pyecharts生成Echarts网页的实例

    2023-02-22 10:19:42
  • Python+Selenium+Webdriver实现自动执行微软奖励积分脚本

    2022-09-13 13:49:07
  • sql server如何得到插入一条记录后最新的ID?

    2009-11-15 20:06:00
  • 浅谈LogMiner的使用方法

    2009-02-28 11:12:00
  • Python函数基础(定义函数、函数参数、匿名函数)

    2022-04-24 05:21:41
  • python批量压缩图像的完整步骤

    2022-05-14 16:10:47
  • OpenCV+python3实现视频分解成图片

    2023-05-30 19:10:55
  • Python实现Mysql数据统计及numpy统计函数

    2024-01-18 13:15:07
  • asp如何计算下载一个文件需要多长时间?

    2009-11-25 20:17:00
  • 深入了解vue-router原理并实现一个小demo

    2024-04-30 10:25:31
  • 详解python文件的操作和异常的处理

    2021-06-07 04:05:37
  • 对Keras自带Loss Function的深入研究

    2021-08-27 03:18:24
  • 你凭什么说你的网站用户体验好

    2011-03-31 17:08:00
  • python+appium自动化测试之如何控制App的启动和退出

    2023-06-24 12:45:01
  • ionic实现带字的toggle滑动组件

    2024-04-16 09:25:45
  • 分析运行中的 Python 进程详细解析

    2021-09-19 14:47:30
  • Django中的CBV和FBV示例介绍

    2022-05-23 10:13:59
  • 在函数间不能传递32个以上参数的疑难问题

    2008-12-31 13:31:00
  • asp之家 网络编程 m.aspxhome.com