The difference between computed, methods, and watched in vue

Source: Internet
Author: User

The difference between computed, methods, and watched in vue

Preface

This article describes the differences between computed, methods, and watched in vue for your reference. Let's take a look at the details below:

Calculation attribute

Similar to common attributes, a computing attribute is bound to a template. When the corresponding data in data changes, the value of the computing attribute also changes.

Methods

Methods is a method. If you call it, the function is executed.

Same:The two achieve the same effect.

Different:The computing attributes are cached based on their dependencies. They will only apply for a new job if their dependencies change. As long as the dependencies remain unchanged, only the previous results are returned and no function execution is performed.

<P> raw data: {msg }}</p> <p> changed data: {changemsg }}</p> var vm = new Vue ({<span style = "white-space: pre"> </span> el: "# example ", data: {<span style = "white-space: pre"> </span> msg: "hello", <span style = "white-space: pre "> </span >}, <span style =" white-space: pre "> </span> computed: {changemsg: function () {return this. msg. split (""). reverse (). join ("");},}

Computed attributes VS watched attributes

Watched attribute: the code is easier to understand. It specifies the value of monitoring and changes other values accordingly.

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 } }})

Computed attribute: the code is simpler.

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

Summary

The above is all about this article. I hope this article will help you learn or use shell. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.