Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Vue's Computed Properties, Methods, and Watchers

Tech 1

Computed Properties vs Methods

computed properties and methods both process data from data, but with key differences. Computed properties cache results based on reactive dependencies, only recalculatign when dependencies change. Methods execute on every call during re-renders.

Use computed properties for expensive operations like large array processing where caching improves performance. Use methods for operations requiring fresh calculations each time, like function() { return Date.now() }.

Computed Propreties vs Watchers

Computed Properties:

  1. Cache results based on reactive dependencies
  2. Don't support asynchronous operations
  3. Derive values from other properties (many-to-one/one-to-one relationships)
  4. Default getter with optional setter

Watchers:

  1. Execute immediately when data changes (no caching)
  2. Suppport asynchronous operations
  3. Receive new and old values as parameters
  4. Trigger side effects when specific data changes (one-to-many relationships)
  5. Can use deep option for object property changes

Watcher Example

<div id="watch-example">
  <p>
    Ask a question:
    <input v-model="question">
  </p>
  <p>{{ answer }}</p>
</div>

<script>
const watchExampleVM = new Vue({
  el: '#watch-example',
  data: {
    question: '',
    answer: 'Ask something!'
  },
  watch: {
    question(newQ, oldQ) {
      this.answer = 'Waiting...'
      this.debouncedFetchAnswer()
    }
  },
  created() {
    this.debouncedFetchAnswer = _.debounce(this.fetchAnswer, 500)
  },
  methods: {
    fetchAnswer() {
      if (!this.question.includes('?')) {
        this.answer = 'Questions need question marks!'
        return
      }
      
      this.answer = 'Processing...'
      axios.get('https://yesno.wtf/api')
        .then(response => {
          this.answer = _.capitalize(response.data.answer)
        })
        .catch(error => {
          this.answer = `API Error: ${error}`
        })
    }
  }
})
</script>
Tags: Vue.js

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.