In Vue.js, watch
and computed
are both used to monitor data changes, but their usage and implementation mechanisms are different. The following are their differences:
✨ Implementation mechanism
watch
What is monitored is a specific data, and when the data changes, the corresponding callback function will be executed; the computed
attribute is like a responsive calculated attribute, which is cached based on the data it depends on. When the dependent data changes, computed
the attribute will automatically update the cache. When the data changes, the corresponding operation will not be performed immediately, but it will wait for the next update opportunity of the task queue.
✨ Applicable scenarios
watch
More suitable for data monitoring with side effects, such as some data that requires background requests;computed
It is more suitable for scenarios where properties are calculated based on existing data (data and props) and need to be cached.
✨ Features
watch
Can monitor multiple data and make corresponding processing when the data changes;computed
Only the cache of calculated attributes is supported. The cache will be automatically updated when the dependent data changes, and can begetter
setsetter
.
✨ Performance
watch
The performance is weak because it monitors changes in a specific data or multiple data. If too much data is monitored, itwatch
will have a negative impact on the performance of the entire application;computed
The performance is better because it is essentially a cache. The cache will only be updated when the data changes, and it will not mindlessly traverse the monitoring data like watch.
✨ Differences in monitoring data
watch
What is monitored is a specific data, which can be a property in the data or a prop passed in from outside the component. When the data changes, the corresponding callback function will be executed.computed
It is a calculated property, which is a property calculated based on existing data (data and props). When used in a template, it is like a responsive property. When the data on which the calculation depends on changes, the calculated attribute will be automatically updated, and the calculated attribute has a caching mechanism. When the data on which the calculated attribute depends does not change, the cached result will be returned directly.
✨Differences in execution timing
watch
When the monitored data changes, the callback function will be executed immediately, which is used to monitor data with asynchronous operations.computed
It is recalculated whenever the dependent data changes, and is used to process data in the template.
✨Differences in implementation methods
watch
It is mainly implemented through$watch
and objects. $watch can monitor changes in a single data, and objects can monitor changes in multiple data.watch
watch
computed
computed
It is implemented through the attribute, which receives an object as a parameter.key
The corresponding name of the calculated attribute in the objectvalue
corresponds to the logical function of the calculated attribute.
In short, watch
it is more flexible and involves a wider range of data, but is slightly inferior in performance; it computed
is more concise and can save performance, but it is only suitable for calculating existing data and returning results. Under normal circumstances computed
It is used to process complex logical operations and is convenient to call in templates; watch
it is used to monitor some data changes, usually used when asynchronous operations need to be performed.