VUE basics (3): computed and watch

One, computed

1.computed is a calculated attribute, that is, a calculated value. It is more used in scenarios where the value is calculated.

2. Computed is cacheable. The calculated value will be cached after the getter is executed. Only after the attribute value it depends on changes, the next time the computed value is obtained will the corresponding getter be called again for calculation.

3.computed is suitable for computing scenarios that consume more performance.

二、watch

1. Watch is more of a "observation" function, similar to the monitoring callback of some data, used to observe the value of props, $emit or this component, and execute the callback for subsequent operations when the data changes.

2. No caching, it will be executed even if the value does not change when the page is re-rendered.

Three, summary

1. When we need to calculate data and rely on other data, then design this data as computed.

2. If you need to do something when a certain data changes, use watch to observe the data change.

Guess you like

Origin blog.csdn.net/imagine_tion/article/details/110680243