Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue3 methods Property

Vue3 methods Property


Vue3 Options API Vue3 Options API

In Vue 3,methods The property is an object used to define the methods available in a component. These methods can be called in the component's template via event binding or other means.

methods The property is typically used to handle user interactions, execute certain logic operations, or trigger component behavior.


Basic Syntax for Using the methods Property

In a Vue 3 component,methods the property is typically defined outside the setup function. It contains an object where each key-value pair represents a method: the key is the method name and the value is its implementation.

Example

export

default

{

  methods

:

{

    methodName

(

)

{

     

// Method logic

   

}

,

    anotherMethod

(

)

{

     

// Logic of another method

   

}

 

}

}


Example: Trigger a Method on Button Click

The following is a simple example showing how to use the methods property to handle a button click event.

Example


Code Analysis

  1. Template Section:

  2. Use @click event binding associates the handleClick method with the button's click event.
  3. When the user clicks the button, thehandleClick method will be called.
  4. - Use the `@click` event binding to associate the `handleClick` method with the button's click event. - When the user clicks the button, the `handleClick` method will be called.
    1. Script Section:

    2. data The function returns an object containing message object property, which is used to store the message to display.
    3. methods The object defines the handleClick method, which modifies the message value.
    4. - The `data` function returns an object containing the `message` property, which stores the displayed message. - The `methods` object defines the `handleClick` method, which modifies the `message` value.

      The Role of the methods Property

      1. Handle User Interactions:

      2. methods The property is commonly used to respond to user interactions such as clicks and inputs.
      3. - The `methods` property is commonly used to respond to user interactions such as clicks and inputs.
        1. Execute Logic Operations:

        2. You can write complex logic code in methods, such as data calculations and conditional judgments.
        3. - You can write complex logic code in methods, such as data calculations and conditional judgments.
          1. Trigger Component Behavior:

          2. Through method calls, you can trigger component state updates, data requests, and other behaviors.
          3. - Through method calls, you can trigger component state updates, data requests, and other behaviors.

            The Difference Between methods and computed

            - Executes the code in the method body every time it is called. - Suitable for event handling or manually triggered operations. - Is only recalculated when its dependent data changes. - Suitable for scenarios requiring dynamically computed results based on data.

            Exercise

            1. Try to add a new method in the methods to clear message .
            2. Modify the example code so that clicking the button displays the current time.

            Vue3 Options API Vue3 Options API