其实 ref 不会创建浅拷贝,一旦你将一个 obj 传递给一个 ref,它就会调用 createReactiveObject 方法来生成一个响应式对象。关于浅参考,您可以使用
shallowref 方法,https://v3.vuejs.org/api/refs-api.html#shallowref
由于您没有提供任何代码,这里仅举一个简单的例子供您参考:
将 cmets 定义为响应式对象,child 不需要为 props 引用:
父组件:
<template>
<Comment v-for="comment in comments" :comment="comment" :key="comment.id"></Comment>
<button @click="addCmt">addCmt</button>
</template>
<script>
import Comment from "./components/Comment";
import {reactive} from "vue";
export default {
name: "Params",
setup() {
const comments = reactive([{id: 1, name: 'a'}, {id: 6, name: 'c'}]);
function addCmt() {
comments.unshift({id: comments.length + 10, name: 'k'});
}
return {
comments,
addCmt
}
},
components: {Comment}
}
</script>
评论组件:
<template>
<div>comments {{ comment.id }}</div>
</template>
<script>
export default {
name: "Comment",
props: ['comment'],
setup(props, ctx) {
const comment = props.comment;
return {comment};
}
}
</script>