Method vs Computed in Vue Method vs Computed in Vue vue.js vue.js

Method vs Computed in Vue


Computed values and methods are very different in Vue and are definitely not interchangeable in most cases.

Computed Property

A more appropriate name for a computed value is a computed property. In fact, when the Vue is instantiated, computed properties are converted into a property of the Vue with a getter and sometimes a setter. Basically you can think of a computed value as a derived value that will be automatically updated whenever one of the underlying values used to calculate it is updated. You don't call a computed and it doesn't accept any parameters. You reference a computed property just like you would a data property. Here's the classic example from the documentation:

computed: {  // a computed getter  reversedMessage: function () {    // `this` points to the vm instance    return this.message.split('').reverse().join('')  }}

Which is referenced in the DOM like this:

<p>Computed reversed message: "{{ reversedMessage }}"</p>

Computed values are very valuable for manipulating data that exists on your Vue. Whenever you want to filter or transform your data, typically you will use a computed value for that purpose.

data:{    names: ["Bob", "Billy", "Mary", "Jane"]},computed:{    startsWithB(){        return this.names.filter(n => n.startsWith("B"))    }}<p v-for="name in startsWithB">{{name}}</p>

Computed values are also cached to avoid repetitively calculating a value that doesn't need to be re-calculated when it hasn't changed (as it might not be in a loop for example).

Method

A method is just a function bound to the Vue instance. It will only be evaluated when you explicitly call it. Like all javascript functions, it accepts parameters and will be re-evaluated every time it's called. Methods are useful in the same situations any function is useful.

data:{    names: ["Bob", "Billy", "Mary", "Jane"]},computed:{    startsWithB(){        return this.startsWithChar("B")    },    startsWithM(){        return this.startsWithChar("M")    }},methods:{    startsWithChar(whichChar){        return this.names.filter(n => n.startsWith(whichChar))    }}

Vue's documentation is really good and easily accessible. I recommend it.


As @gleenk asked for a practical example to make evident the cache and dependency differences between methods and computed properties, I'll show a simple scenario:

app.js

new Vue({    el: '#vue-app',    data: {        a: 0,        b: 0,        age: 20    },    methods: {        addToAmethod: function(){            console.log('addToAmethod');            return this.a + this.age;        },        addToBmethod: function(){            console.log('addToBmethod');            return this.b + this.age;        }    },    computed: {        addToAcomputed: function(){            console.log('addToAcomputed');            return this.a + this.age;        },        addToBcomputed: function(){            console.log('addToBcomputed');            return this.b + this.age;        }    }});

Here we have 2 methods and 2 computed properties that perform the same task. The methods addToAmethod & addToBmethod and the computed properties addToAcomputed & addToBcomputed all add +20 (i.e. the age value) to either a or b. Regarding the methods, they are both called every time an action has been performed on any of the listed properties, even if the dependencies for one specific method have not changed. For the computed properties, the code is executed only when a dependency has changed; for example, one of the specific property values that refers to A or B will trigger addToAcomputed or addToBcomputed, respectively.

The method and computed descriptions seem pretty similar, but as @Abdullah Khan has already specified it, they are not the same thing! Now let's try to add some html to execute everything together and see where the difference is.

The Method case demo

new Vue({    el: '#vue-app',    data: {        a: 0,        b: 0,        age: 20    },    methods: {        addToAmethod: function(){            console.log('addToAmethod');            return this.a + this.age;        },        addToBmethod: function(){            console.log('addToBmethod');            return this.b + this.age;        }    }});
<!DOCTYPE html>    <html>        <head>            <meta charset="utf-8">            <title>VueJS Methods - stackoverflow</title>            <link href="style.css" rel="stylesheet" />            <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.11/vue.min.js"></script>            </head>        <body>            <div id="vue-app">                <h1>Methods</h1>                <button v-on:click="a++">Add to A</button>                <button v-on:click="b++">Add to B</button>                <p>Age + A = {{ addToAmethod() }}</p>                <p>Age + B = {{ addToBmethod() }}</p>            </div>        </body>                <script src="app.js"></script>    </html>