Vue in watch / computed using the method and its introduction scene

watch

  1. effect
  • Defined in the data used to monitor data, when the data defined in the data changes, then watch the key will trigger
  • the object is a watch
    watch: {}
  • You can watch a plurality of types of keys
  1. Use (Key)
  • method
  watch: {
    fn () {}
  }
  • Object: (depth monitor)
    watch: {
      fn: {
        handler(){},
        deep: true
      }
    }
    

Computed Property

  1. Use
  • Inside storage method
      computed: {
        fn () {
          return ''   //必须要有返回值
        }
      }
    
  • Objects inside the store
      computed: {
        newName: {
          get () {
            return '' //get里面要有return
          },
          set ( val ) { //val就是修改后的值
    
          }
        }
      }
    
    • NOTE: The above get, set us unify a name called: memory, others called getter / setter
    • Both get set, there is a subject, and there was the class
    • get set is to calculate the properties of this understanding is wrong

watch vs computed

  1. watch is used to monitor certain data, when data is changed, it will automatically trigger the watch, then we can make some tasks
  2. is computed in order to expose a global variable, the global variable is generated by a certain logic
  3. When selected watch? What is the choice computed? When selecting methods?
  • Large amount of data, and we choose to watch asynchronous operation scenarios: on Raja load, pull down to refresh
  • computd use two meet on it
    • Exposure to a similar global variable data
    • Logic may be processed
  • methods of use: Event Program program (user interaction)

Guess you like

Origin blog.csdn.net/HelloWord182/article/details/93337036