Summary
In this lesson, you learned that:
v-modelconnects a field with a ref both ways: what's typed goes into the ref, and a change of the ref shows in the field. It does what:valueand@inputdo together, and in the template, it's written without.value.- It works on text fields and on a
textarea(never{{ }}between its tags), on aselect, whose ref gets thevalueof the chosen option (:valuewhen the options come fromv-for), and on radio buttons, whose ref gets thevalueof the checked one. - A checkbox with a ref of
trueorfalseanswers yes or no. Several checkboxes with the same array fill it with the values of the checked ones. - The value of a field is always text.
.trimremoves the spaces at its start and end, and.numberturns it into a number. Fields withtype="number"get.numberby themselves, and radio buttons and checkboxes get numbers with:value. @submit.preventon a form runs your function when the form is sent. Without.prevent, the page loads again, and the app starts over.- To add an item from a form, push an object with its own
idinto the array, then empty the fields by setting their refs. - Error messages are in the template, with
v-if, and a ref says when to show them.:disabledkeeps a button from being clicked until the form is filled in.
In the next lesson, Computed Values and Watchers, you'll write values made from other values once, such as the total of an order, and run code whenever a value changes, for example to save the order.
But first, a little practice: Corner Café takes table bookings. App.vue has only a heading, and you'll write the form yourself. A tip: keep the message about the booking in a ref of its own, so that it stays when the name field is emptied. And v-for over a number makes the options quickly.
Instructions
- 1
Write a form with a text field
#guestfor the name, a drop-down list#peoplefor 1 to 6 people (2 at the start), and a buttonBook. When the form is sent, showTable for 4 booked for Anna.under it, with the number of people and the trimmed name, and empty the name field. - 2
Disable the
Bookbutton while the name is empty.