vue3报错 Vue received a Component which was made a reactive object.

发布时间 2023-03-24 18:04:04作者: 蓓蕾心晴

报错信息如下:

Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with `markRaw` or using `shallowRef` instead of `ref`. 

大致意思是,这个数组里头的对象不是响应式的,推荐使用 shallowReactive 或 shallowRef,即浅层作用形式,仅最外层是响应式,以此来提升性能。

见官方文档

shallowRef() 常常用于对大型数据结构的性能优化或是与外部的状态管理系统集成。

shallowReactive 和 reactive() 不同,这里没有深层级的转换:一个浅层响应式对象里只有根级别的属性是响应式的。属性的值会被原样存储和暴露,这也意味着值为 ref 的属性不会被自动解包了。

谨慎使用: 浅层数据结构应该只用于组件中的根级状态。请避免将其嵌套在深层次的响应式对象中,因为它创建的树具有不一致的响应行为,这可能很难理解和调试。