We have seen some of the basic operations in vuejs . Now move on to advanced area. In the following example we will learn how to process a form in Vue
We have a simple form component with two controls which has indeed three section template, Script and a Style.
<template>
.........
<form @submit.prevent="createContent">
<label for="newContent">
<textarea
placeholder="content here"
id="newContent"
rows="7"
v-model="newContent"
/>
<Button > Save</button>
</template>
We have a very simple form with one text area and a button to save. I removed the styling elements for simplicity.
Bind the textarea
To bind the textarea we have to use v-model directive of vue , so that we can access the value.
v-model="newContent"
Event handling
In the form tag , we can specify a event handler for the button click. To use a method as event handler for submit we can use @sumit=”<method>”, for a click event we have to use @click=”methodName”.
We also need to prevent the default form submission event which occurs when the use press enter key in the input field. For that we used submit.prevent
The Script
Everything behind the scene belongs to the script. Here we have to create the
- property for binding model
- method for event handling
<script>
export default {
name: "Form",
created() {},
data() {
newContent= ''
},
methods: {
createContent() {
if (this.newContent) {
//code to handle the event
// clear the field after sibmission
this.newContent = ''
}
},
},
}
}
Now you know how to handle forms in vue, simple isn’t it ?
The following vue post posts may help you
- How reactivity works in Vuejs - How to use reactive feature in Vuejs
- Create a desktop version of Quasar-Vue app using Electron - How to build desktop version of Quasar Vue app using Electron
- Create PolarArea chart component in Vue - How to Create a PolarArea Chart component in Vue using vue-chartjs module
- Create Radar component in Vue - How to Create a Radar Chart component in Vue using vue-chartjs module
- Create Horizontal Bar chart component in Vue - How to Create a HoruzotalBar Chart component in Vue using vue-chartjs module
- Create Scatter chart component in Vue - How to Create a scatter Chart component in Vue using vue-chartjs module
- Create Bubble chart component in Vue - How to Create a bubble Chart component in Vue using vue-chartjs module
- Quasar UI framework for Vue - About Quasar Vuejs UI framework and How to create and run Vuejs app with Quasar CLI tool
- Component with same name in Vuejs - How to dealing with component has same name in Vuejs
- Create Pie chart component in Vue - How to Create a Pie Chart component in Vue using vue-chartjs module