Using Vue 3 Composition API for Forms

Tutorial 5 of 5

Introduction

In this tutorial, we will explore Vue 3's Composition API for form handling. Our goal is to provide you with a flexible and efficient way to manage complex form logic and state.

By the end of this tutorial, you will:
- Understand the basics of Vue 3's Composition API
- Learn how to handle form state and logic using Composition API

Prerequisites:
- Basic knowledge of JavaScript
- Familiarity with Vue.js is helpful but not mandatory

Step-by-Step Guide

Vue 3's Composition API is a set of additive, function-based APIs that allows flexible composition of component logic. It was introduced to handle complex component logic in a more efficient way.

Setup Vue 3 Project

First, you need to set up a new Vue 3 project if you don't have one already. You can do this using Vue CLI:

vue create vue3-composition-api-forms

Create Form Component

Next, create a new file FormComponent.vue in your components directory. This component will handle your form logic.

Import Composition API Functions

In your FormComponent.vue, import the necessary Composition API functions:

import { ref } from 'vue';

Create Reactive Variables

We use ref() to create reactive variables. These are equivalent to data() in Options API.

const name = ref('');
const email = ref('');

Code Examples

Example 1: Basic Form Handling

Let's start with a basic form handling. Here's a simple form with two fields: name and email.

<template>
  <form @submit.prevent="submitForm">
    <label>Name:</label>
    <input v-model="name" type="text">

    <label>Email:</label>
    <input v-model="email" type="text">

    <button type="submit">Submit</button>
  </form>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const name = ref('');
    const email = ref('');

    const submitForm = () => {
      console.log('Name:', name.value);
      console.log('Email:', email.value);
    };

    return { name, email, submitForm };
  },
};
</script>

In this example, v-model binds the input fields to the reactive variables. The submitForm function logs the values of the fields when the form is submitted.

Summary

We've covered the basics of using Vue 3's Composition API for form handling. You've learned how to create reactive variables using ref() and how to handle form submission.

Next steps for learning include exploring more complex form handling scenarios, such as form validation.

Practice Exercises

  • Exercise 1: Create a form with more fields, such as address and phone number. Handle the submission of this form.
  • Solution: You can simply add more reactive variables for the new fields and include them in v-model and submitForm.
  • Tips: Consider creating a resetForm function that resets all fields after form submission.

  • Exercise 2: Add basic validation to your form. For example, make the email field required and validate it's in the correct format.

  • Solution: You can create a validation reactive variable that stores validation errors. Update this variable in submitForm and display the errors in your template.
  • Tips: For more complex validation, consider using a library like VeeValidate.

Remember, practice is key in mastering any new concept. Happy coding!