1 IntroductionWhen creating a Vue instance, you can pass in an options object const vm = new Vue({ data: { msg: 'hello' }, computed: {}, methods: {}, watch: {} }) This options object can specify a lot of options (or attributes), and data-related options include but are not limited to Among them, 2 Basic usage Use <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Methods</title> <!-- Import vue.js --> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script> </head> <body> </body> <script> </script> </html> 2.1 Methods The functions defined in the <body> <div id="example"> <!-- Display: a:1 --> <p>a:{{ plus() }}</p> </div> </body> <script> const vm = new Vue({ el: "#example", data: { a: 0, }, methods: { plus: function () { return this.a + 1; }, }, }); console.log(vm); // View the console output of vm, you can see that it has a method: plus: ƒ (), ⚠️Note that it is a method console.log(vm.plus()); // Access the method directly through the vm instance, output: 1 </script> The function in 2.2 computed properties The function defined in the <body> <div id="example"> <!-- Display: a:1 --> <p>a:{{ plus }}</p> </div> </body> <script> const vm = new Vue({ el: "#example", data: { a: 0, }, computed: { plus: function () { return this.a + 1; }, }, }); console.log(vm); // // Check the vm output by the console, you can see that it has an attribute: plus:1, ⚠️Note that it is an attribute</script> At first glance it seems that In fact, the difference between the two has been reflected by printing the vm instance and the access method:
In addition, unlike methods, computed properties can be updated responsively as the data they depend on changes, that is, when a changes, 2.3 watch listener The key-value pair in the During the <body> <div id="example"> <!-- Display: a:1 --> <p>a:{{ a }}</p> </div> </body> <script> const vm = new Vue({ el: "#example", data: { a: 0, }, watch: a: function () { console.log("a has changed"); // Because the value of a has changed, the callback function executes console.log(this.a); }, }, }); vm.a = 1; // Manually change the value of a here</script> 3 Differences between the three 3.1 Methods vs. Computed PropertiesIn addition to the two differences mentioned in 2.2, the most important difference is:
The following table summarizes the differences between the two:
3.2 Computed Properties vs Listeners
If a value needs to be calculated from one or more data, use a calculated property The listening property is mainly used to monitor changes in a certain value and then perform the required logical processing; in addition, when it is necessary to perform asynchronous or costly operations when data changes, the listening property is more useful. For specific examples, see the vue document - listener This is the end of this article about the differences between Vue's You may also be interested in:
|
<<: Use of kubernetes YAML files
>>: Cross-browser development experience summary (I) HTML tags
A website uses a lot of HTML5 and CSS3, hoping th...
Recently, I found a fun hover animation from the ...
1. Use CSS to draw a small pointed-corner chat di...
Disable Build Partition expressions do not suppor...
MySQL View the maximum number of connections and ...
Put your own web project in the webapps directory...
1. The difference between TEXT and BLOB The only ...
ClickHouse is an open source column-oriented DBMS...
Achieve resultsImplementation Code html <input...
Parent File import React, { useState } from '...
This article mainly records the effect of using j...
1. Introduction Supervisor is a general process m...
Table of contents From father to son: 1. In the s...
Today I will introduce the most basic functions of...
NProgress is the progress bar that appears at the...