vue3中computed的传参
父组件
点击父组件的按钮:更改disabledBool,子组件的删除按钮会响应式吗?
答复:会的。
<template>
<div>
<TestCom :disabledBool="disabledBool"></TestCom>
<el-button type="primary" @click="changeDisabledBoolHandler">更改disabledBool</el-button>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import TestCom from '@/components/TestCom.vue'
const disabledBool = ref(false)
function changeDisabledBoolHandler(){
disabledBool.value = !disabledBool.value
}
</script>
子组件
<template>
<div>
<div v-for="item in tableData" :key="item.id" class="box">
<div>{{item.name}}</div>
props.disabledBool: {{ props.disabledBool }}
<div><el-button link :disabled="getDisabled(item)">删除</el-button></div>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue';
const props = defineProps({
disabledBool: {
type: Boolean,
default: false
}
})
let tableData= ref([])
function serveApi(){
setTimeout(() => {
tableData.value= [
{id:1,name:'张三'},
{id:2,name:'李四'},
{id:3,name:'王五'},
]
}, 1200);
}
serveApi()
const getDisabled = computed(()=>{
// console.log('此时有缓存',props.disabledBool)
console.log("disabledBool",props.disabledBool)
return function(item){
// 下面的代码多次执行, 此时不在时缓存计算了
console.log('item',item)
return item.id === 1 && props.disabledBool
}
})
</script>
<style scoped>
.box{
display: flex;
align-items: center;
}
</style>

你发现了什么

computed失去了缓存的特性。
computed 传参后 return 里面的函数代码,不在是缓存计算了。
其实就是第一次return一个函数function,
这个return的函数中有我们传递的参数。
return外面的有缓存,return里面的没有缓存了
const showSerNameAA = computed(() => {
console.log('此时有缓存',allServerInfo )
return function (serValue) {
// 下面的代码多次执行, 此时不在时缓存计算了
console.log('没有缓存 == >', serValue)
return serValue
}
})
为啥getDisabled函数也会执行
父组件的代码不进行任何的更改。
可以将上传computed传参去掉,
使用下面的这一种写法哈.
<template>
<div>
<div v-for="item in tableData" :key="item.id" class="box">
<div>{{item.name}}</div>
<div><el-button link :disabled="getDisabled(item)">删除</el-button></div>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue';
const props = defineProps({
disabledBool: {
type: Boolean,
default: false
}
})
let tableData= ref([])
function serveApi(){
setTimeout(() => {
tableData.value= [
{id:1,name:'张三'},
{id:2,name:'李四'},
{id:3,name:'王五'},
]
}, 1200);
}
serveApi()
function getDisabled(item: { id: number }) {
console.log('我也会执行',item)
return item.id === 1 && props.disabledBool
}
</script>
<style scoped>
.box{
margin-bottom: 10px;
display: flex;
}
</style>
为啥父组件,点击按钮,子组件的getDisabled函数也会执行。
因为 Vue 的响应式会触发了子组件重新渲染。
重新渲染时模板里的函数getDisabled会被调用。
整条链路如下:
父组件点击按钮
↓
disabledBool.value = !disabledBool.value // ref 值(disabledBool)变了
↓
父组件模板里 :disabledBool="disabledBool" // 动态 props 绑定变了
↓
子组件 props.disabledBool 更新了 // Vue 自动同步
↓
子组件被标记为: 需要重新渲染(re-render)
↓
Vue 执行子组件的 render 函数
↓
模板里所有表达式重新求值 ← 重点在这里!
↓
:disabled="getDisabled(item)" 被执行了 ← 看到的现象
一句话总结: 父组件改了 props → 子组件重新渲染 → 模板重新求值 → 函数被调用
什么情况下,父组件改了 props → 子组件不会重新渲染呢?
情况一:父组件的值"换了但没真变"
const disabledBool = ref(false)
// 父组件点击时
disabledBool.value = false // 没变!还是 false
Vue 的响应式系统内部会做一层 相等比较 (Object.is 语义),
新旧值完全相等时, 根本不会触发派发更新 ,子组件自然不会重渲染。
情况二:prop 绑定不是动态的
<!-- ❌ 没有 v-bind / : ,这是静态字符串字面量 -->
<Child disabledBool="false" />
<!-- ✅ 有 : 才是响应式绑定 -->
<Child :disabledBool="disabledBool" />
情况三:子组件被 v-memo 跳过了(Vue 3.2+)
<!-- 父组件模板 -->
<Child :disabledBool="disabledBool" v-memo="[disabledBool]" />
情况四:shallowRef /shallowReactive 只关心顶层引用
// 父组件
const user = shallowRef({ name: 'zs' })
// 子组件
<Child :user="user" />
// 父组件这样改 → 不触发子组件重渲染
user.value.name = 'ls'
// 父组件这样改 → 触发(顶层引用换了)
user.value = { name: 'ls' }
Vue 3 默认ref /reactive 是 深响应式 的,
但shallowRef /shallowReactive 只追踪一层。
遇见问题,这是你成长的机会,如果你能够解决,这就是收获。
出处:https://www.cnblogs.com/IwishIcould/
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,或者关注博主,在此感谢!
万水千山总是情,打赏5毛买辣条行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主(っ•̀ω•́)っ✎⁾⁾!
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!
如果文中有什么错误,欢迎指出。以免更多的人被误导。

浙公网安备 33010602011771号