毁抨句 发表于 2025-6-3 10:43:52

vue3第二次传递数据方法无法获取到最新的值


使用reactive父组件第二次传递给子组件的数据:方法中可以获取到最新数据

<template>

   
      <h1>子组件</h1>
      <child :infoObj='infoObj' ref="childRef"></child>
   
    <button @click='updateHandler'>跟新值</button>
   
      <h1>父页面</h1>
      <p>{{ infoObj }}</p>
   

</template><template>

    <h1> {{ props.infoObj }}</h1>

</template>
使用ref父组件第二次传递给子组件的数据:不能获取到最新的数据

<template>

   
      <h1>子组件</h1>
      <child :infoObj='infoObj' ref="childRef"></child>
   
    <button @click='updateHandler'>跟新值</button>
   
      <h1>父页面</h1>
      <p>{{ infoObj }}</p>
   

</template><template>

    <h1> {{ props.infoObj }}</h1>

</template>
办法1:将数据作为函数的参数进行传递

<template>

   
      <h1>子组件</h1>
      <child :infoObj='infoObj' ref="childRef"></child>
   
    <button @click='updateHandler'>跟新值</button>
   
      <h1>父页面</h1>
      <p>{{ infoObj }}</p>
   

</template><template>

    <h1> {{ props.infoObj }}</h1>

</template>
解决办法2:在调用方法时使用 nextTick

<template>

   
      <h1>子组件</h1>
      <child :infoObj='infoObj' ref="childRef"></child>
   
   
      <h1>父页面</h1>
      <p>{{ infoObj }}</p>
   
    <button @click='updateHandler'>跟新值</button>

</template><template>

    <h1> {{ props.infoObj }}</h1>

</template>
结论

使用ref父组件第二次传递给子组件的数据(基本数据和引用数据):不能获取到最新的数据。
使用reactive和ref传递参数给子组件,为啥ref第二次子组件无法获取最新的数据?而reactive可以

在 Vue 3 中,reactive 和 ref 在传递给子组件时的行为有所不同。
这也说明了 reactive 和 ref 是有区别的(屁话)。
ref 和 reactive 的区别

1,ref可以试用于任何数据类型,而reactive只适用于对象类型。
2,在js模块ref获取值,设置值,需要点value, ‌在模板中使用不需要点value。 而reactive都不需要。
3,ref可以完全替换整个对象,不会失去响应式。
reactive不能直接替换整个对象(否则会失去响应式)。需要逐个修改属性或使用 Object.assign
4,返回值不同。ref返回一个‌包装对象‌。reactive返回一个‌Proxy 对象‌
ref完全替换 不会失去响应式

<template>
<button type="button" @click="updateHandler">更改数据</button>
<p>数据{{ objRef }}</p>
</template>reactive不能直接替换整个对象(会失去响应式)

const objReactive = reactive({ a: 1 })
// 错误方式(失去响应性)
objReactive = { b: 2 }

// 正确方式 或者逐个修改属性
Object.assign(objReactive, { b: 2 })[错误]:ref解构不会失去响应式。reactive解构或展开会失去响应式。[这句话不正确]

ref和reactive解构都会失去响应式。都需要通过toRefs 或者toRef 来进行解决。
reactive 解构会失去响应式

<template>
<button type="button" @click="updateHandler">更改数据</button>
<p>数据{{ name }} {{age}}</p>
</template>
ref 解构会失去响应式

<template>

    <p>Name: {{ name }}</p>
    <p>Age: {{ age }}</p>
    <button @click="changeName">Change Name</button>

</template>
toRefs()​解构ref,解构后仍然保持响应式

<template>

    <p>Name: {{ name }}</p>
    <p>Age: {{ age }}</p>
    <button @click="changeName">Change Name</button>

</template>
toRef()​解构reactive,解构后仍然保持响应式

<template>
<button type="button" @click="updateHandler">更改数据</button>
<p>数据{{ name }} {{age}}</p>
</template>
toRefs()​

官网:将一个响应式对象转换为一个普通对象。
这个普通对象的[每个属性]都是指向源对象[相应属性的] ref。
每个单独的 ref 都是使用 toRef() 创建的。
我的理解:
toRefs 可以把一个响应式对象转换为普通的对象。
该普通对象的每一个值都是ref。
由于变成了ref,所以我们使用每个属性的时候需要点value。
ref和reactive的使用场景

ref 适合于基本数据类型,reactive适合于对象类型。
ref 适合完全替换整个对象
我喜欢用ref定义基本数据类型和数组。对象使用reactive。
ref的本质

我理解的ref本质上是reactive的再封装。
使用reactive定义响应式数据时,若数据不是对象类型直接就返回了。
就不会进行后续的数据响应式处理了。
这也就是我只用reactive定义对象型响应式数据的原因
                                                                                                                                                                                微信                                                                                                本文版权归作者所有,欢迎转载,未经作者同意须保留此段声明,在文章页面明显位置给出原文连接
                        如果文中有什么错误,欢迎指出。以免更多的人被误导。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: vue3第二次传递数据方法无法获取到最新的值