This is the 24th day of my participation in Gwen Challenge


Interviewer: Tell me the difference between computed and watch

I:… 💥 💥


Computed properties computed:

  1. Support the cache, onlyDependency data changes, will be recalculated
  2. Asynchrony not supportedFor a computed asynchronous operation, data changes cannot be monitored
  3. Computed property values doDefault cache walk, computed properties are cached based on their reactive dependencies, i.eBased on the data ofDeclared orData in props passed by the parent componentThe value obtained by calculation
  4. If a property is computed by something else, that property depends on something else, it’s a many-to-one or one-to-one, usually computed
  5. If the computed attribute value is a function, the default is to use the GET method; Function of theThe return value is the property value of the property; In computed, attributes have a GET and a set method, which are called when data changes.


Listening property Watch:

  1. Cache is not supported. When data changes, corresponding operations are directly triggered.

  2. Watch supports asynchrony;

  3. The listening function takes two arguments, the first of which is the latest value; The second argument is the value before input;

  4. When an attribute changes, you need to perform the corresponding operation. A couple more;

  5. The listening data must be the data declared in data or passed by the parent component in props. When the data changes, other operations are triggered. The function takes two arguments,

    1. immediateComponent loading triggers callback function execution immediately,
    2. deep: Deep listening, for detectionObject internal valueFor complex types of data, such as changes in the contents of the object in the array. Note that listening for changes in the array does not need to do this. Note: Deep cannot listen for array changes or object additions, as in vUE array mutations, only when triggered in a responsive manner.

The listening object can also be written as a string

This approach is most useful when asynchronous or expensive operations need to be performed when data changes. This is the biggest difference between computed and computed.