vue3中reactive注意点(系列四)
reactive
-
reactive
是 Vue3 中提供的实现响应式数据的方法。 -
在 Vue2 中响应式数据是通过 defineProperty 来实现的,在 Vue3 中响应式数据是通过 ES6 的
Proxy
来实现的。具体参照Vue双向数据绑定,Vue3 的 Proxy 和 defineProperty 的比较。 -
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 迫不得已我硬着头皮查看了keepalive的源代码,解决了线上的问题
- vue.js3.x 使用vue3-count-to 组件(到可视区域才start数字滚动 vue@3.2.33 / vue3-count-to@1.1.2)
- vue3快速入门:学习Vue3为什么要学习TypeScript?使用 TypeScript 的 10 个理由
- vue2和vue3有哪些区别和不同2
- vue3组合式Composition API之setup函数的具体用法
- Vue3快速上手资料!手把手带你彻底学会Vue3,超详细讲解!不怕学不会!【建议收藏】【尚硅谷Vue2.0+Vue3.0全套教程丨vuejs从入门到精通vue3笔记资料】
- 只能用watchEffect才能监听的到vue3 监听父组件传过来的值
- 用Vue3构建企业级前端应用,TS能让你更轻松点
- vue3 - 关闭eslint提示参数必须定义类型
- vue3+ts创建二维数组响应式变量及赋值
- vue3如何获取绑定在组件上的 textarea DOM元素
- 从0搭建Vue3组件库(四): 如何开发一个组件
- Vue3中的组件通信方式
- vue3 hooks 组件封装
- 深入Vue3源码,看看Vue.use后究竟发生了什么?
- Vue3 对比Vue2,你找到哪些变化?
- vue3面试题:2022 最新前端 Vue 3.0 面试题及答案(持续更新中……)
- Vue3项目中使用富文本编辑器
- Vue3悬浮返回主页按钮设计与实现
- Vue3 reactive / customRef 快速理解掌握
- 掌握Vue3模板语法,助你轻松实现高效Web开发