Computed and Composition API in Vue 3

Posted by

In Vue 3, the Composition API provides developers with a more flexible way to manage component logic compared to the Options API. One key concept in the Composition API is computed properties, which allow you to create reactive data that updates automatically when its dependencies change.

In this tutorial, we will explore how to use computed properties in Vue 3 with the Composition API. Let’s get started by creating a new Vue 3 project and setting up a basic component with computed properties.

Step 1: Setting up a Vue 3 project
To create a new Vue 3 project, you can use the Vue CLI by running the following command in your terminal:

vue create my-vue3-project

Follow the prompts to select the default options for creating a new Vue 3 project. Once the project is created, navigate to the project directory and install the Vue Composition API plugin by running the following command:

npm install @vue/composition-api

Step 2: Creating a component with computed properties
In your Vue 3 project, create a new component file named ComputedExample.vue in the components directory. In this file, add the following code to define a basic component with computed properties:

<template>
  <div>
    <h1>{{ message }}</h1>
    <p>Character count: {{ characterCount }}</p>
  </div>
</template>

<script>
import { ref, computed } from '@vue/composition-api';

export default {
  setup() {
    const message = ref('Hello, Vue 3!');
    const characterCount = computed(() => message.value.length);

    return {
      message,
      characterCount
    };
  }
};
</script>

In this code snippet, we import the ref and computed functions from the @vue/composition-api package. We then define a message reactive variable using the ref function and a characterCount computed property using the computed function.

The message variable is initialized with the string ‘Hello, Vue 3!’, while the characterCount computed property calculates the length of the message and updates automatically when the message changes.

Step 3: Using the computed properties in a parent component
To use the ComputedExample component in a parent component, you can import and include it in the parent component’s template. For example, you can create a new component named App.vue and add the following code:

<template>
  <div>
    <computed-example />
  </div>
</template>

<script>
import ComputedExample from './components/ComputedExample.vue';

export default {
  components: {
    ComputedExample
  }
};
</script>

Now, when you run your Vue 3 project using npm run serve, you should see the text "Hello, Vue 3!" and the character count displayed in the browser. Try updating the message variable in the ComputedExample component to see the character count automatically update.

In this tutorial, we explored how to use computed properties in Vue 3 with the Composition API. Computed properties allow you to create reactive data that updates automatically based on its dependencies, providing a flexible way to manage component logic in Vue 3. Experiment with computed properties in your Vue 3 project to see how they can improve your development workflow.

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@gabrieljauneribera9931
3 months ago

Kralho baita aula hein

@whisperscribe
3 months ago

Boa Pablo!

@ebonalume
3 months ago

Ótimo, muito bom.😃