Vue Event Handling
Estimated reading: 4 minutes 376 views

Vue Methods – Define Interactive Behavior in Vue.js (2025 Guide)


Introduction – What Are Methods in Vue?

In Vue.js, methods are used to handle user interactions, perform logic processing, and manage dynamic behavior in your component. Defined within the methods option, they are accessible from the template and can also be reused internally.

In this guide, you’ll learn:

  • How to declare and use Vue methods
  • Best practices for method design
  • The difference between methods, computed, and watchers
  • Examples of methods in form handling, UI interaction, and data processing

What is the methods Option in Vue?

In a Vue component, the methods object contains functions that can be:

  • Called from template events using v-on or @
  • Invoked from other methods
  • Used to encapsulate reusable logic

Example:

export default {
  data() {
    return {
      name: 'Vue'
    };
  },
  methods: {
    greet() {
      alert(`Hello, ${this.name}!`);
    }
  }
}

Explanation:

  • greet() is defined in methods
  • this.name accesses reactive data
  • Can be triggered via a button click in the template

Using Methods in Templates

HTML + Method:

<button @click="sayHello">Click Me</button>

Vue Component:

methods: {
  sayHello() {
    console.log('Hello from Vue!');
  }
}

How It Works:
Clicking the button calls sayHello().


Passing Arguments to Methods

You can pass parameters and access the $event object:

<button @click="handleClick('Save', $event)">Save</button>
methods: {
  handleClick(label, event) {
    console.log(`${label} button clicked`, event.target);
  }
}

Reusable Logic with Methods

Vue methods are not only for UI events—they also serve as reusable functions:

methods: {
  calculateTotal(items) {
    return items.reduce((sum, item) => sum + item.price, 0);
  }
}

Call it from lifecycle hooks, watchers, or even other methods.


Methods vs Computed vs Watch

FeaturePurposeCached?Best For
methodsRun logic on call NoEvent handlers, logic reuse
computedReactive, auto-update if deps change YesDerived data
watchObserve changes to data NoTrigger logic on data change

Real-World Example – Form Validation

<template>
  <form @submit.prevent="submitForm">
    <input v-model="email" type="email" />
    <button>Submit</button>
  </form>
</template>

<script>
export default {
  data() {
    return { email: '' };
  },
  methods: {
    submitForm() {
      if (this.email.includes('@')) {
        alert('Email is valid!');
      } else {
        alert('Invalid email');
      }
    }
  }
}
</script>

Functionality:

  • submitForm() validates email before submitting
  • Triggered by form’s submit event

Calling Methods Within Methods

Vue allows method chaining:

methods: {
  greet() {
    console.log('Hi!');
  },
  greetAndLog() {
    this.greet();
    console.log('Greeted the user');
  }
}

Methods with Props and State

You can access props, data, and computed inside methods:

props: ['price'],
methods: {
  logPriceWithTax() {
    console.log(this.price * 1.18);
  }
}

Best Practices for Vue Methods

Use camelCase for method names
Keep methods short and specific
Delegate business logic to utility functions when needed
Avoid modifying unrelated state from within methods
Add default values and validation for method parameters


Summary – Recap & Next Steps

Vue methods are the backbone of your component’s logic. They handle everything from button clicks to form submissions and reusable utilities. Mastering methods gives you clean, readable, and modular components.

Key Takeaways:

  • Declare all methods inside the methods object
  • Use this to access component data and props
  • Use for UI events, logic execution, and code reuse
  • Prefer computed for derived values and watch for reactive listeners

Real-World Relevance:
Almost every Vue component uses methods—for input handling, state updates, HTTP requests, and more.


FAQ Section

What is the purpose of methods in Vue?

It stores functions that you can call from your templates, events, or other internal component logic.


Can methods access data and props?

Yes. Use this.propertyName to access any reactive data or props inside a method.


What’s the difference between a method and a computed property?

A method runs on every call, while a computed property is cached and only recalculates when dependencies change.


How do you pass arguments to Vue methods?

Directly in the template using parentheses:

<button @click="doSomething('value')">Click</button>

Can methods call other methods?

Absolutely. Use this.methodName() to chain logic within your Vue instance.


Share Now :
Share

Vue Methods

Or Copy Link

CONTENTS
Scroll to Top