vue3第二次傳遞數(shù)據(jù)方法無(wú)法獲取到最新的值的解決方法

使用reactive父組件第二次傳遞給子組件的數(shù)據(jù):方法中可以獲取到最新數(shù)據(jù)
<template>
<div>
<div>
<h1>子組件</h1>
<child :infoObj='infoObj' ref="childRef"></child>
</div>
<button @click='updateHandler'>跟新值</button>
<div>
<h1>父頁(yè)面</h1>
<p>{{ infoObj }}</p>
</div>
</div>
</template>
<script setup lang="ts">
import child from '@/components/child.vue'
import { ref,reactive } from 'vue'
let infoObj = reactive({
name:'張三',
age:26
})
const childRef = ref()
function updateHandler(){
infoObj.name = '李四'
infoObj.age = 28
// 跟新值后,調(diào)用父組件的方法。
childRef.value.getData()
}
</script><template>
<div>
<h1> {{ props.infoObj }}</h1>
</div>
</template>
<script setup lang="ts">
let props = defineProps({
infoObj:Object,
})
function getData(){
console.log('infoObj', props.infoObj)
}
defineExpose({
getData
})
</script>
使用ref父組件第二次傳遞給子組件的數(shù)據(jù):不能獲取到最新的數(shù)據(jù)
<template>
<div>
<div>
<h1>子組件</h1>
<child :infoObj='infoObj' ref="childRef"></child>
</div>
<button @click='updateHandler'>跟新值</button>
<div>
<h1>父頁(yè)面</h1>
<p>{{ infoObj }}</p>
</div>
</div>
</template>
<script setup lang="ts">
import child from '@/components/child.vue'
import { ref } from 'vue'
let infoObj = ref({
name:'張三',
age:26
})
const childRef = ref()
function updateHandler(){
infoObj.value = {
name:'李四',
age:28
}
// 跟新值后,調(diào)用父組件的方法??聪耮etData是否可以獲取到最新的值
childRef.value.getData()
}
</script><template>
<div>
<h1> {{ props.infoObj }}</h1>
</div>
</template>
<script setup lang="ts">
let props = defineProps({
infoObj:Object,
})
function getData(){
console.log('infoObj', props.infoObj)
}
defineExpose({
getData
})
</script>
辦法1:將數(shù)據(jù)作為函數(shù)的參數(shù)進(jìn)行傳遞
<template>
<div>
<div>
<h1>子組件</h1>
<child :infoObj='infoObj' ref="childRef"></child>
</div>
<button @click='updateHandler'>跟新值</button>
<div>
<h1>父頁(yè)面</h1>
<p>{{ infoObj }}</p>
</div>
</div>
</template>
<script setup lang="ts">
import child from '@/components/child.vue'
import { ref } from 'vue'
let infoObj = ref({
name:'張三',
age:26
})
const childRef = ref()
function updateHandler(){
infoObj.value = {
name:'李四',
age:28
}
// 將數(shù)據(jù)作為函數(shù)的參數(shù)進(jìn)行傳遞
childRef.value.getData(infoObj.value)
}
</script><template>
<div>
<h1> {{ props.infoObj }}</h1>
</div>
</template>
<script setup lang="ts">
let props = defineProps({
infoObj:Object,
})
function getData(mess:any){
console.log('infoObj', props.infoObj)
console.log('將數(shù)據(jù)作為函數(shù)的參數(shù)進(jìn)行傳遞:mess', mess)
}
defineExpose({
getData
})
</script>
解決辦法2:在調(diào)用方法時(shí)使用 nextTick
<template>
<div>
<div>
<h1>子組件</h1>
<child :infoObj='infoObj' ref="childRef"></child>
</div>
<div>
<h1>父頁(yè)面</h1>
<p>{{ infoObj }}</p>
</div>
<button @click='updateHandler'>跟新值</button>
</div>
</template>
<script setup lang="ts">
import child from '@/components/child.vue'
import { nextTick, ref } from 'vue'
let infoObj = ref({
name:'張三',
age:26
})
const childRef = ref()
function updateHandler(){
infoObj.value = { name: '李四', age: 28 };
// 推薦在這里使用nextTick
nextTick(() => {
childRef.value.getData()
})
}
</script><template>
<div>
<h1> {{ props.infoObj }}</h1>
</div>
</template>
<script setup lang="ts">
let props = defineProps({
infoObj:Object,
})
function getData(){
// 或者在這里使用nextTick。
console.log('getData 方法獲取值', props.infoObj.name, props.infoObj.age)
}
defineExpose({
getData
})
</script>
結(jié)論
使用ref父組件第二次傳遞給子組件的數(shù)據(jù)(基本數(shù)據(jù)和引用數(shù)據(jù)):不能獲取到最新的數(shù)據(jù)。
使用reactive和ref傳遞參數(shù)給子組件,為啥ref第二次子組件無(wú)法獲取最新的數(shù)據(jù)?而reactive可以
在 Vue 3 中,reactive 和 ref 在傳遞給子組件時(shí)的行為有所不同。這也說(shuō)明了 reactive 和 ref 是有區(qū)別的(屁話)。
ref 和 reactive 的區(qū)別
1,ref可以試用于任何數(shù)據(jù)類型,而reactive只適用于對(duì)象類型。2,在js模塊ref獲取值,設(shè)置值,需要點(diǎn)value, 在模板中使用不需要點(diǎn)value。 而reactive都不需要。3,ref可以完全替換整個(gè)對(duì)象,不會(huì)失去響應(yīng)式。reactive不能直接替換整個(gè)對(duì)象(否則會(huì)失去響應(yīng)式)。需要逐個(gè)修改屬性或使用 Object.assign4,返回值不同。ref返回一個(gè)包裝對(duì)象。reactive返回一個(gè)Proxy 對(duì)象
ref完全替換 不會(huì)失去響應(yīng)式
<template>
<button type="button" @click="updateHandler">更改數(shù)據(jù)</button>
<p>數(shù)據(jù){{ objRef }}</p>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const objRef = ref({ age: 1 })
function updateHandler(){
//完全替換 不會(huì)失去響應(yīng)式
objRef.value = { age: 100 }
}
</script>reactive不能直接替換整個(gè)對(duì)象(會(huì)失去響應(yīng)式)
const objReactive = reactive({ a: 1 })
// 錯(cuò)誤方式(失去響應(yīng)性)
objReactive = { b: 2 }
// 正確方式 或者逐個(gè)修改屬性
Object.assign(objReactive, { b: 2 })[錯(cuò)誤]:ref解構(gòu)不會(huì)失去響應(yīng)式。reactive解構(gòu)或展開(kāi)會(huì)失去響應(yīng)式。[這句話不正確]
ref和reactive解構(gòu)都會(huì)失去響應(yīng)式。都需要通過(guò)toRefs 或者toRef 來(lái)進(jìn)行解決。
reactive 解構(gòu)會(huì)失去響應(yīng)式
<template>
<button type="button" @click="updateHandler">更改數(shù)據(jù)</button>
<p>數(shù)據(jù){{ name }} {{ age}}</p>
</template>
<script setup lang="ts">
import { reactive } from 'vue';
const state = reactive({ name: '張三', age: 20 })
// reactive解構(gòu)會(huì)失去響應(yīng)式
let { name, age } = state
function updateHandler(){
// 更新數(shù)據(jù)后,頁(yè)面不會(huì)跟新
name = '王麻子'
age = 1000
}
</script>
ref 解構(gòu)會(huì)失去響應(yīng)式
<template>
<div>
<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>
<button @click="changeName">Change Name</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
// 使用 ref 創(chuàng)建響應(yīng)式對(duì)象
const user = ref({
name: 'Alice',
age: 25
})
// 解構(gòu) ref 對(duì)象 - 會(huì)失去響應(yīng)式,視圖不會(huì)跟新
let { name, age } = user.value
function changeName() {
name = 'Bob' // 直接修改解構(gòu)出來(lái)的屬性
}
</script>
toRefs()?解構(gòu)ref,解構(gòu)后仍然保持響應(yīng)式
<template>
<div>
<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>
<button @click="changeName">Change Name</button>
</div>
</template>
<script setup>
import { ref,toRefs } from 'vue'
// 使用 ref 創(chuàng)建響應(yīng)式對(duì)象
const user = ref({
name: 'Alice',
age: 25
})
// 通過(guò)toRefs解構(gòu)不會(huì)失去響應(yīng)式
let { name, age } = toRefs(user.value)
function changeName() {
name.value = '大大再大' // 直接修改解構(gòu)出來(lái)的屬性
}
</script>
toRef()?解構(gòu)reactive,解構(gòu)后仍然保持響應(yīng)式
<template>
<button type="button" @click="updateHandler">更改數(shù)據(jù)</button>
<p>數(shù)據(jù){{ name }} {{ age}}</p>
</template>
<script setup lang="ts">
import { reactive, toRefs } from 'vue';
const state = reactive({ name: '張三', age: 20 })
// 通過(guò)toRefs解構(gòu)不會(huì)失去響應(yīng)式
let { name, age } = toRefs(state)
function updateHandler(){
// 更新數(shù)據(jù)后,頁(yè)面不會(huì)跟新
name.value = '王麻子'
age.value = 1000
}
</script>
toRefs()?
官網(wǎng):將一個(gè)響應(yīng)式對(duì)象轉(zhuǎn)換為一個(gè)普通對(duì)象。這個(gè)普通對(duì)象的[每個(gè)屬性]都是指向源對(duì)象[相應(yīng)屬性的] ref。每個(gè)單獨(dú)的 ref 都是使用 toRef() 創(chuàng)建的。我的理解:toRefs 可以把一個(gè)響應(yīng)式對(duì)象轉(zhuǎn)換為普通的對(duì)象。該普通對(duì)象的每一個(gè)值都是ref。由于變成了ref,所以我們使用每個(gè)屬性的時(shí)候需要點(diǎn)value。
ref和reactive的使用場(chǎng)景
ref 適合于基本數(shù)據(jù)類型,reactive適合于對(duì)象類型。ref 適合完全替換整個(gè)對(duì)象我喜歡用ref定義基本數(shù)據(jù)類型和數(shù)組。對(duì)象使用reactive。
ref的本質(zhì)
我理解的ref本質(zhì)上是reactive的再封裝。使用reactive定義響應(yīng)式數(shù)據(jù)時(shí),若數(shù)據(jù)不是對(duì)象類型直接就返回了。就不會(huì)進(jìn)行后續(xù)的數(shù)據(jù)響應(yīng)式處理了。這也就是我只用reactive定義對(duì)象型響應(yīng)式數(shù)據(jù)的原因
到此這篇關(guān)于vue3第二次傳遞數(shù)據(jù)方法無(wú)法獲取到最新的值的文章就介紹到這了,更多相關(guān)vue3第二次傳遞數(shù)據(jù)方法無(wú)法獲取到最新的值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue計(jì)算屬性computed方法內(nèi)傳參方式
這篇文章主要介紹了vue計(jì)算屬性computed方法內(nèi)傳參方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue使用Axios進(jìn)行跨域請(qǐng)求的方法詳解
在開(kāi)發(fā)現(xiàn)代?Web?應(yīng)用時(shí),前端和后端通常分離部署在不同的服務(wù)器上,這就會(huì)引發(fā)跨域請(qǐng)求問(wèn)題,所以本文將詳細(xì)介紹如何在?Vue?項(xiàng)目中使用?Axios?發(fā)起跨域請(qǐng)求時(shí)解決跨域問(wèn)題的相關(guān)資料,需要的朋友可以參考下2024-09-09
Vue.js 帶下拉選項(xiàng)的輸入框(Textbox with Dropdown)組件
這篇文章主要介紹了Vue.js 帶下拉選項(xiàng)的輸入框(Textbox with Dropdown)組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
vue實(shí)現(xiàn)點(diǎn)擊復(fù)制到粘貼板
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)點(diǎn)擊復(fù)制到粘貼板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
el-upload http-request使用 多個(gè)文件上傳攜帶其他參數(shù)方式
這篇文章主要介紹了el-upload http-request使用 多個(gè)文件上傳攜帶其他參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue全局過(guò)濾器概念及注意事項(xiàng)和基本使用方法
這篇文章主要給大家分享了vue全局過(guò)濾器概念及注意事項(xiàng)和基本使用方法,下面文字圍繞vue全局過(guò)濾器的相關(guān)資料展開(kāi)具體的詳細(xì)內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-11-11
vue如何使用element ui表格el-table-column在里面做判斷
這篇文章主要介紹了vue如何使用element ui表格el-table-column在里面做判斷問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

