Vue Methods

Vue Tutorial

Vue Methods

In Vue.js, Vue Methods are functions defined inside a Vue component that are used to handle events, perform actions, and execute logic when something happens in the UI.

 What are Vue Methods?

Vue methods are regular JavaScript functions that:

  • Respond to events (click, submit, keyup, etc.)

  • Perform calculations

  • Update data

  • Execute business logic

They are defined inside the methods option of a Vue app or component.


 Basic Syntax


 Example-1: Simple Method


 

  •  The method runs when the button is clicked.

 Example-2: Method with Data

  •  Methods can modify reactive data using this.

 Example-3: Passing Parameters


 Example-4: Using $event

  • $event gives access to the native DOM event.

 Example 5: Methods in Template Expressions

  • This method runs every time the component re-renders.

 Methods vs Computed Properties (Very Important)

FeatureMethodsComputed
ExecutionEvery renderCached
PerformanceLowerHigher
Use forActions / eventsCalculated data
ParametersYesNo

Use methods for:

  • Click handlers

  • API calls

  • Form submission

Use computed for:

  • Derived data

  • Filtering

  • Calculations based on state


 Methods with Forms


 Calling Methods from Another Method


 Common Mistakes

 Using arrow functions:

 Correct:


 Best Practices

  •  Keep methods simple and readable
  •  Use clear names (handleSubmit, incrementCount)
  •  Avoid heavy logic in templates
  •  Prefer computed for repeated calculations

 Summary Table

TaskUse
Handle clickMethod
Submit formMethod
Update dataMethod
Derived valueComputed
Event logicMethod

 Conclusion

Vue methods are the action handlers of your application.
They connect user interaction with application logic and are essential for building interactive Vue apps.

You may also like...