Chapter 10: Vue Methods

Vue Methods — like we’re sitting together in a Hyderabad café, laptop open, building something fun step by step 😄

In Vue, “methods” are simply the functions you define to handle user interactions, business logic, or any action that needs to be triggered (clicks, form submits, timers, API calls, etc.).

But here’s the important thing in 2026:

Vue gives you two main ways to write these methods, and which one you use depends on whether you’re using the Options API (classic style) or the Composition API (modern, recommended style).

  • Options API → methods live in a methods: { } object
  • Composition API (especially with <script setup>) → methods are just normal JavaScript functions declared at the top level

The Composition API + <script setup> is now the default recommendation for almost all new Vue 3 code in 2025–2026. The Options API is still fully supported (great for legacy code or very small components), but most tutorials, jobs, and large apps have moved to Composition.

I’ll teach you both so you understand old code, new code, and can choose wisely.

1. Methods in Options API (Classic Vue 2 / early Vue 3 style)

This is how methods looked in most Vue 2 apps and many Vue 3 apps that haven’t migrated yet.

vue

Key characteristics:

  • All methods inside methods: { }
  • Access data with this.count, this.logAction(…)
  • this refers to the component instance
  • Good for: small components, people coming from Vue 2, quick prototypes

2. Methods in Composition API + <script setup> (Modern 2026 way – recommended!)

This is how almost everyone writes Vue components today.

vue

Key differences & advantages:

  • No methods: { } object
  • No this at all! → much cleaner, less confusing
  • Functions are declared normally with function name() or const name = () => { }
  • Everything top-level in <script setup> is automatically available in <template>
  • Easier to extract logic into composables (reusable functions)

3. Methods with Parameters & Event Object

Modern style example:

vue

4. When Methods Become Computed or Watchers (common confusion)

vue

Rule of thumb 2026:

  • Want to do something (change state, call API, show alert) → method (plain function)
  • Want a derived value that auto-updates → computed()
  • Want to react to change → watch() or watchEffect()

Quick Comparison Table – Methods in 2026

Aspect Options API Composition API + <script setup> Winner in 2026
Declaration methods: { increment() { … } } function increment() { … } Composition
Accessing state this.count++ count.value++ Composition (no this)
Boilerplate More (export default, methods block) Minimal Composition
Reusability Harder to extract logic Easy → composables Composition
TypeScript support Okay Excellent Composition
Still used? Legacy code, small widgets New projects, large apps

Real-World Advice from Hyderabad 2026

  • New project? → Use <script setup> + plain functions (methods)
  • Maintaining old Vue 2 / early Vue 3 code? → Learn Options API methods: { }
  • Large team? → Composition makes code easier to split, test, reuse
  • Want to share logic between components? → Turn methods into composables
JavaScript
vue

Practice challenge:

  • Build a form with submit method
  • Add validation method
  • Create a reset method
  • Extract counter logic to a composable

Any part unclear? Want full example with API call method? Or how to type methods in TypeScript? Or difference when using arrow functions?

Just tell me — we’ll keep building your Vue skills step by step 🚀

Happy coding from Webliance, Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *