Article From:https://www.cnblogs.com/jasonlai2016/p/9969563.html

1.Calculated attribute

For any complex logic, you should use computational properties

<div id="example">
  <p>Original message: "{{ message }}"</p>
  <p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>
var vm = new Vue({
  el: '#example',
  data: {
    message: 'Hello'
  },
  computed: {
    // Getter for calculating attributesReversed Message: function () {// `this` points to the VM instanceReturn this. message. sPlit ('). reverse (). join (')}}}

a.Computing Attribute Cache vs Method

Computational properties are cached based on their dependencies.

Why do we need caching? Suppose we have a computational attribute A with high performance overhead, which needs to traverse a huge array and do a lot of computations. Then we may have other computational properties that depend on A. Without caching, we will inevitably execute A get multiple timesTer! If you don’t want a cache, use a method instead.

b.Computing attributes vs listening attributes

It’s usually better to use computational attributes rather than imperative watch callbacks (listening attributes)

var vm = new Vue({
  el: '#demo',
  data: {
    firstName: 'Foo',
    lastName: 'Bar',
    fullName: 'Foo Bar'
  },
  watch: {
    firstName: function (val) {
      this.fullName = val + ' ' + this.lastName
    },
    lastName: function (val) {
      this.fullName = this.firstName + ' ' + val
    }
  }
})
var vm = new Vue({
  el: '#demo',
  data: {
    firstName: 'Foo',
    lastName: 'Bar'
  },
  computed: {
    fullName: function () {
      return this.firstName + ' ' + this.lastName
    }
  }
})

c.Setter for calculating attributes

By default, getter is the only computational attribute, but you can also provide a setter if you need to:

// ...
computed: {
  fullName: {
    // getter
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // setter
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}

2.Monitor

Used when data needs to change.Executing asynchrony or overheadOperation.

<div id="watch-example">
  <p>
    Ask a yes/no question:
    <input v-model="question">
  </p>
  <p>{{ answer }}</p>
</div>
<!-- Because the ecosystem of AJAX libraries and general tools is already quite rich, the Vue core code is not duplicated.-->
<!-- Provide these features to keep it lean. It also gives you the freedom to choose tools that you are more familiar with.-->
<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
<script>
var watchExampleVM = new Vue({
  el: '#watch-example',
  data: {
    question: '',
    answer: 'I cannot give you an answer until you ask a question!'
  },
  watch: {
    // If `question'changes, the function runs
    question: function (newQuestion, oldQuestion) {
      this.answer = 'Waiting for you to stop typing...'
      this.debouncedGetAnswer()
    }
  },
  created: function () {
    // `_.debounce` Lodash is a function that limits the operating frequency.
    // In this example, we want to limit the frequency of access to yesno.wtf/api
    // AJAX The request will not be sent until the user has finished typing. Want to know more about
    // `_.debounce` Knowledge of functions (and their close relatives `throttle'),
    // Refer to: https://lodash.com/docs#debounce
    this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
  },
  methods: {
    getAnswer: function () {
      if (this.question.indexOf('?') === -1) {
        this.answer = 'Questions usually contain a question mark. ;-)'
        return
      }
      this.answer = 'Thinking...'
      var vm = this
      axios.get('https://yesno.wtf/api')
        .then(function (response) {
          vm.answer = _.capitalize(response.data.answer)
        })
        .catch(function (error) {
          vm.answer = 'Error! Could not reach the API. ' + error
        })
    }
  }
})
</script>

 

Leave a Reply

Your email address will not be published. Required fields are marked *