Common interview Questions — Talk about the difference between COMPUTED and Watch in Vue. The answer to this question is formulaic. Let’s translate computed and watch respectively, and talk about their characteristics, and then the difference can be solved.

computed

Computed is used for simple computation. The authors of VUE thought that too much logical computation in the UI of a page would make page maintenance difficult, so they invented computed property. It wraps the responsive property dependencies in functions, and vUE calculates computed values every time it runs. It is then placed in the cache, where the value of the variable is normally retrieved. The cache is changed and re-rendered when its calculated dependencies change.

// To demonstrate the two dependencies, assume that the variable num is a computed attribute, computed: {//1. Num :function(){return XXX} //2. Getter,setter num:{get:function(){return XXX}, Set :function(value){return XXX}}Copy the code

watch

Watch is used to listen for changes in values on Vue instance objects when we want the Vue to do something to change the data on the page. When the monitored variable in Watch changes, watch will receive the changed amount and then pass in two values to watch, one is the old value and the other is the new value. Watch can use variables such as immediate and deep to achieve more functions

{a:function(newVal,oldVal){console.log(' current value is ${newVal}, previous value was ${oldVal} ')}} Watch :{a:function(newVal){console.log(newVal)}, immediate:true}Copy the code

That’s how I understand the problem