DEV Community

Cover image for Vue.js for Beginners 2024 #VueJs Part 4 : Form & Event Listener
KyDev
KyDev

Posted on • Edited on

Vue.js for Beginners 2024 #VueJs Part 4 : Form & Event Listener

Why Use Vue.js for Forms?

Advantages of Vue.js in Creating Forms:

  • Simple Data Binding: Vue.js uses v-model to bind data between form inputs and component data, making synchronization easy.
  • Reactivity: Any changes to the model data automatically update the view, ensuring users always see the most current data without writing a lot of code.
  • Form Validation: You can easily add validation to form inputs using computed properties or libraries like VeeValidate.
  • Reusable Components: You can create form components that can be reused in various parts of your application, improving code readability and maintainability.
  • Integration with Third-Party Libraries: Vue.js can be integrated with various libraries to handle more complex forms, such as Vuex for state management.

Advantages of Vue.js in Handling Event Listeners:

  • Easy Syntax: Using v-on or the shorthand @ to add event listeners is very intuitive and easy to read.
  • Supports Various Event Types: Vue.js allows you to handle a wide range of events, such as clicks, inputs, and submits, in a consistent manner.
  • Event Modifiers: Vue.js provides modifiers to manage event behavior more efficiently, such as .stop to prevent event bubbling.
  • Reactivity on Events: You can easily change component state based on events that occur, thanks to Vue.js's powerful reactivity system.
  • Supports Custom Events: When using components, you can easily create and listen for custom events to communicate between components.

Steps to Create a Form in Vue.js

  1. Set Up a Vue.js Project If you don’t have a Vue.js project yet, you can create one using Vue CLI. Here’s how to set up a new project:
npm install -g @vue/cli
vue create my-form-app
cd my-form-app
npm run serve
Enter fullscreen mode Exit fullscreen mode
  1. Create a Form Component Once your project is running, let’s create a simple form component. In the src/components folder, create a new file called MyForm.vue.
<template>
  <div>
    <h1>Simple Form</h1>
    <form @submit.prevent="handleSubmit">
      <div>
        <label for="name">Name:</label>
        <input type="text" id="name" v-model="form.name" required />
      </div>
      <div>
        <label for="email">Email:</label>
        <input type="email" id="email" v-model="form.email" required />
      </div>
      <button type="submit">Submit</button>
    </form>
    <p v-if="submitted">Form has been submitted!</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: '',
        email: ''
      },
      submitted: false
    };
  },
  methods: {
    handleSubmit() {
      console.log('Submitted data:', this.form);
      this.submitted = true;
      // Reset form
      this.form.name = '';
      this.form.email = '';
    }
  }
};
</script>
Enter fullscreen mode Exit fullscreen mode
  1. Use the Form Component Now, we need to add the MyForm component to our application. Open the src/App.vue file and include the component we just created.
<template>
  <div id="app">
    <MyForm />
  </div>
</template>

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

export default {
  components: {
    MyForm
  }
};
</script>
Enter fullscreen mode Exit fullscreen mode
  1. Run the Application Now, run your application with the command:
npm run serve
Enter fullscreen mode Exit fullscreen mode

Vue.js is an excellent choice for developing forms and managing event listeners in web applications.

Using Vue.js, you can easily create interactive and responsive forms. You can further enhance this form by adding validation, state management, and other features as needed for your application.

Happy coding!!!

Top comments (0)