This time I will bring you the difference between method and computed in Vue. What are the precautions for using method and computed in Vue? The following is a practical case, let's take a look.
Both computed and methods in the configuration parameters of new Vue can handle a large amount of logic code, but when to use whichattribute, you need to distinguish carefully to use vue correctly.
computed is called a computed attribute. As the name suggests, calculation must return a calculation result. Therefore, when we need to process a large amount of logic, but in the end we want to obtain the final result, we can use computed;In order to illustrate the difference between method and computed, here I would like to first take a look at what the computed attribute says on the vue official website: Let’s look at an example:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="javascript/vue.min.js"></script> </head> <body> <p id="app"> //直接在模板中绑定表达式 <p>{{message.split('').reverse().join('')}}</p> //运用计算属性 <p>message反转之后的结果:{{reverseMessage}}</p> </p> <script> var vm=new Vue({ el:"#app", data:{ message:"hello" }, computed:{ reverseMessage:function(){ return this.message.split('').reverse().join(''); } } }) </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="javascript/vue.min.js"></script> </head> <body> <p id="app"> <p>{{message}}</p> //直接在模板中绑定表达式 <p>{{message.split('').reverse().join('')}}</p> //运用计算属性 <p>{{reverseMessage}}</p> //运用methods方式 <p>{{methodMessage()}}</p> </p> <script> var vm=new Vue({ el:"#app", data:{ message:"hello" }, computed:{ reverseMessage:function(){ return this.message.split('').reverse().join(''); } }, methods:{ methodMessage:function () { return this.message.split('').reverse().join(''); } } }) </script> </body> </html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Display of http requests and loading in Vue2.0
How does Vue2.0 realize bidirectional component data Binding
The above is the detailed content of The difference between method and computed in Vue. For more information, please follow other related articles on the PHP Chinese website!