经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » React » 查看文章
vue3.0 Reactive数据更新页面没有刷新的问题
来源:jb51  时间:2023/4/21 8:57:16  对本文有异议

vue3.0 Reactive数据更新页面没有刷新

vue 3.0 ref 和 Reactive 数据响应式,以及使用 Reactive 数据已更新但页面没有同步刷新异常

Vue 3.0 中我们使用 reactive() 定义的响应式数据的时候,当我们对象再次赋值,我们发现数据已经修改成功,但是页?并没有自动渲染成最新的数据;

这时我们可以改成 ref () 或者对 reactive() 绑定的数据类型下点功夫;

  • ref()

ref() 接受一个内部值并返回一个响应式且可变的 ref 对象。ref 对象仅有一个 .value property,指向该内部值

  1. <template>
  2. ? <div>
  3. ? ? <button @click="changeMsg">更改数据</button>
  4. ? ? <div>{{ message }}</div>
  5. ? </div>
  6. </template>
  7.  
  8. <script setup lang="ts">
  9. import {ref} from 'vue'
  10.  
  11. /**
  12. ?* ? ref() 基础用法
  13. ?*/
  14. ?
  15. let message = ref<string | number>("测试数据")
  16.  
  17. /**
  18. ?* ? 更改 ref 数据
  19. ?*/
  20. ??
  21. const changeMsg = () => {
  22. ? ?message.value = "更改测试数据"
  23. }
  24. </script>
  • reactive()

reactive() 主要时用来绑定一些复杂的数据类型,比如(对象、数组) ;它不可以绑定普通的数据类型,否则会报错;如果我们需要绑定普通的数据类型,建议使用上面的 ref()

  1. <template>
  2. ? <div>
  3. ? ? <button @click="changeObj">更改数据</button>
  4. ? ? <div> {{obj.data}} </div>
  5. ? ? <div> {{obj.dataBoolean}} </div>
  6. ? ? <div> {{obj.dataArr}} </div>
  7. ? </div>
  8. </template>
  9.  
  10. <script setup lang="ts">
  11. import {reactive} from 'vue'
  12.  
  13. /**
  14. ?* ? reactive() 基础用法
  15. ?*/
  16. ?
  17. const obj = reactive({
  18. ? ? data: '',
  19. ? ? dataBoolean: false,
  20. ? ? dataArr: <number[]>[],
  21. })
  22.  
  23. /**
  24. ?* ? 更改 reactive() 数据
  25. ?*/
  26. ?const changeObj = () => {
  27. ? ? ?obj .data = '测试数据'
  28. ? ? ?obj .dataBoolean = true
  29. ? ? ?obj .dataArr = [1, 2, 3, 4, 5, 6]
  30. ?}
  31.  
  32. </script>

vue3.0中的reactive用法

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

在 Vue2 中响应式数据是通过 defineProperty 来实现的,

在 Vue3 中响应式数据是通过 ES6 的 Proxy来实现的。

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

如果给 reactive 传递了其它对象

  • 默认情况下,修改对象无法实现界面的数据绑定更新。
  • 如果需要更新,需要进行重新赋值。(即不允许直接操作数据,需要放个新的数据来替代原数据)

在 reactive 使用基本类型参数

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

  1. <template>
  2. <div>
  3. <p>{{msg}}</p>
  4. <button @click="c">button</button>
  5. </div>
  6. </template>
  7. <script>
  8. import { reactive } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup() {
  12. let msg = reactive(0)
  13. function c() {
  14. console.log(msg);
  15. msg ++;
  16. }
  17. return {
  18. msg,
  19. c
  20. };
  21. }
  22. }
  23. </script>

08utpj.png

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

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

08uN1s.png

出现提示

value cannot be made reactive: 0

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

  1. <template>
  2. <div>
  3. <p>{{msg.num}}</p>
  4. <button @click="c">button</button>
  5. </div>
  6. </template>
  7. <script>
  8. import { reactive } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup() {
  12. let msg = reactive({
  13. num: 0
  14. })
  15. function c() {
  16. console.log(msg);
  17. msg.num ++;
  18. }
  19. return {
  20. msg,
  21. c
  22. };
  23. }
  24. }
  25. </script>

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

08u8AS.png

在控制台打印消息

08uGtg.png

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

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

  1. <template>
  2. <div>
  3. <p>{{msg.num.inner}}</p>
  4. <button @click="c">button</button>
  5. </div>
  6. </template>
  7. <script>
  8. import { reactive } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup() {
  12. let msg = reactive({
  13. num: {
  14. inner: 0
  15. }
  16. })
  17. function c() {
  18. console.log(msg);
  19. msg.num.inner ++;
  20. }
  21. return {
  22. msg,
  23. c
  24. };
  25. }
  26. }
  27. </script>

08uJhQ.png

数组变化也不在话下。

  1. <template>
  2. <div>
  3. <p>{{msg}}</p>
  4. <button @click="c">button</button>
  5. </div>
  6. </template>
  7. <script>
  8. import { reactive } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup() {
  12. let msg = reactive([1, 2, 3])
  13. function c() {
  14. console.log(msg);
  15. msg[0] += 1;
  16. msg[1] = 5;
  17. }
  18. return {
  19. msg,
  20. c
  21. };
  22. }
  23. }
  24. </script>

08uUcn.png

在-reactive-使用-date-参数在 reactive 使用 Date 参数

如果参数不是数组、对象,而是稍微奇怪一点的数据类型,例如说 Date ,那么麻烦又来了。

  1. <template>
  2. <div>
  3. <p>{{msg}}</p>
  4. <button @click="c">button</button>
  5. </div>
  6. </template>
  7. <script>
  8. import { reactive } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup() {
  12. let msg = reactive(new Date())
  13. function c() {
  14. console.log(msg);
  15. msg.setDate(msg.getDate() + 1);
  16. console.log(msg);
  17. }
  18. return {
  19. msg,
  20. c
  21. };
  22. }
  23. }
  24. </script>!

08uwn0.png

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

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

  1. <template>
  2. <div>
  3. <p>{{msg.date}}</p>
  4. <button @click="c">button</button>
  5. </div>
  6. </template>
  7. <script>
  8. import { reactive } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup() {
  12. let msg = reactive({
  13. date: new Date()
  14. });
  15. function c() {
  16. console.log(msg);
  17. msg.date.setDate(msg.date.getDate() + 1);
  18. console.log(msg);
  19. }
  20. return {
  21. msg,
  22. c
  23. };
  24. }
  25. }
  26. </script>

也仍然不起效果。

08u0BV.png

08uB7T.png

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

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

  1. <template>
  2. <div>
  3. <p>{{msg.date}}</p>
  4. <button @click="c">button</button>
  5. </div>
  6. </template>
  7. <script>
  8. import { reactive } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup() {
  12. let msg = reactive({
  13. date: new Date()
  14. });
  15. function c() {
  16. console.log(msg);
  17. msg.date.setDate((msg.date.getDate() + 1));
  18. msg.date = new Date(msg.date);
  19. console.log(msg);
  20. }
  21. return {
  22. msg,
  23. c
  24. };
  25. }
  26. }
  27. </script>

这里我采用了拷贝的方案重新赋值了 msg.date,界面成功发生了变化(日期 + 1)。

08urAU.png

响应式代理 vs. 原始对象

值得注意的是,reactive() 返回的是一个源对象的 Proxy,它和源对象是不相等的:

  1. const raw = {}
  2. const proxy = reactive(raw)
  3. // 代理和原始对象不是全等的
  4. console.log(proxy === raw) // false

只有代理是响应式的,更改原始的对象不会触发更新。因此,使用 Vue 的响应式系统的最佳实践是 仅使用代理作为状态

为保证访问代理的一致性,对同一个对象调用 reactive() 会总是返回同样的代理,而对代理调用 reactive() 则会返回它自己:

  1. // 在同一个对象上调用 reactive() 会返回相同的代理
  2. console.log(reactive(raw) === proxy) // true
  3. // 在一个代理上调用 reactive() 会返回它自己
  4. console.log(reactive(proxy) === proxy) // true

这个规则对深层级的对象也适用。依靠深层响应性,响应式对象内的深层级对象依然是代理:

  1. const proxy = reactive({})
  2. const raw = {}
  3. proxy.nested = raw
  4. console.log(proxy.nested === raw) // false

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号