物体的浅表手表有什么用?

发布于 2025-02-10 13:53:20 字数 277 浏览 1 评论 0 原文

上一个问题/code>一个整体对象,必须使用 {deep:true} 选项。

我从该要求中得出,在某些情况下,将在对象上使用浅观看

这种手表的典型情况是什么?为了触发回调需要更改?

In a previous question, I learned that in order to watch a whole object, the {deep: true} option must be used.

I deduce from that requirement that there is a case where one would use a shallow watch on an object.

What are the typical cases for such a watch? What needs to change in order for the callback to be triggered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦上情书 2025-02-17 13:53:20

深度手表用于跟踪对象属性的更改。显然,它具有性能影响。在大多数情况下,观看简单数据类型而不是对象。因此,默认情况下启用它是没有道理的。

因此,如果您不需要跟踪对象属性的更改,则无需使用深度手表。

仅当对象分配总共更改时,对象的浅使用才有意义。

  1. 无需进行DeepWatch:
const searchString=ref('');
watch(searchString, () => {
   //perform a search 
})
  1. Deep Watch是必须检测更改的必要条件:
const filters=ref({name:'',price:'',country:''});
watch(filters, () => {
 //perform a filter operation depending on filter options
}, { deep: true });

在第二个示例中,您需要将 efferters.value 设置为新对象,因为您不使用 deep> deep

Deep watch is for tracking changes on an objects properties. Obviously it has a performance impact. Most of the time simple data types are watched instead of objects. So enabling it by default makes no sense.

So if you do not need to track changes of an objects properties you do not need to use deep watch.

Shallow use for an object is only makes sense if the object assignment changes totaly.

  1. No need for deepWatch:
const searchString=ref('');
watch(searchString, () => {
   //perform a search 
})
  1. Deep watch is necessery to detect changes:
const filters=ref({name:'',price:'',country:''});
watch(filters, () => {
 //perform a filter operation depending on filter options
}, { deep: true });

In the second example you need to set filters.value to a new object each time is you do not use deep

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文