This tutorial aims to guide you through the process of creating forms using Vue.js. Vue.js is a popular JavaScript framework used for building user interfaces. By the end of this tutorial, you will have a clear understanding of how to construct interactive forms in Vue.js, which are crucial components of many web applications.
You will learn how to:
Prerequisites:
Vue.js forms are created using a combination of standard HTML form elements and Vue.js directives. The most common directives used with forms are v-model
and v-on
.
v-model
: This is a two-way binding directive. It keeps the input element and the Vue.js data property in sync.v-on
: This directive listens for DOM events. In the context of forms, it can be used to listen for the submit event.Let's create a simple form to see these directives in action.
Example 1: Basic Form
Let's create a basic form with a text input and a submit button.
<template>
<div>
<form>
<label for="name">Name:</label>
<input type="text" id="name" v-model="name">
<button type="submit">Submit</button>
</form>
</div>
</template>
<script>
export default {
data() {
return {
name: ''
}
}
}
</script>
In the code snippet above:
v-model="name"
: This creates a two-way binding between the text input and the name
data property. Any changes to the input field will update the name
property and vice versa.name
property in the data
function which will hold the value of the input field.Example 2: Handling Form Submission
Now, let's handle form submission using the v-on
directive.
<template>
<div>
<form v-on:submit.prevent="submitForm">
<label for="name">Name:</label>
<input type="text" id="name" v-model="name">
<button type="submit">Submit</button>
</form>
</div>
</template>
<script>
export default {
data() {
return {
name: ''
}
},
methods: {
submitForm() {
alert(`Form submitted with name ${this.name}`);
}
}
}
</script>
In this example:
v-on:submit.prevent="submitForm"
: This listens for the submit event and calls the submitForm
method. The .prevent
modifier prevents the default form submission behavior which causes the page to refresh.submitForm
method shows an alert with the submitted name.In this tutorial, you've learned how to create a form in Vue.js, bind data to form elements using v-model
, and handle form submission with v-on
. Next, you could learn about form validation and how to handle different types of form inputs like checkboxes and radio buttons.
Here are some additional resources:
- Vue.js Guide on Form Input Bindings
- Vue.js API on v-model
Exercise 1:
Create a form with two fields, "First Name" and "Last Name". Display the full name elsewhere on the page as the inputs change.
Exercise 2:
Add a "Submit" button to the form you created in exercise 1. When the form is submitted, prevent the page from refreshing and instead, display an alert with the full name.
Exercise 3:
Enhance the form from exercise 2 to include an email field. On form submission, validate that the email field is not empty and contains an '@' symbol. If the validation fails, display an appropriate error message.
Solutions will vary, but the key here is to practice using v-model
for two-way data binding and v-on
for event handling. Keep experimenting and happy coding!