Get the App
SLTechnology News&Howtos  ›  Development  › 

The difference and Application scenario of computed and watch in vue

Shulou Source: shulou.com Published: 2022-06-01 03:19:55 11月12日 Update

This article mainly shows you "the difference and application scenario between computed and watch in vue". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "the difference and application scenario between computed and watch in vue".

What is the difference and application scenario between computed and watch?

Computed: it is a computational attribute, which depends on other attribute values, and the value of computed is cached. Only when the value of the attribute it depends on changes, the value of computed will be recalculated the next time the value of computed is obtained. Watch: it is more like a "observation" function, similar to the listening callback of some data, which performs callbacks for subsequent operations whenever the monitored data changes. Use scenarios:

When we need to do numerical calculations and rely on other data, we should use computed because we can take advantage of the cache feature of computed to avoid having to recalculate every time we get a value.

When we need to perform asynchronous or expensive operations when the data changes, we should use watch, using the watch option to allow us to perform asynchronous operations (accessing an API), limit how often we perform the operation, and set the intermediate state before we get the final result. These are things that computational properties cannot do.

The above is all the content of this article entitled "the difference and Application scenario between computed and watch in vue". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Scenarios attributes data content articles more caching changes learning help monitoring larger useful only overhead numeric easy to understand organization properties status Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology OPPO Reno macOS Docker