Introduction to Vue 3

v-model Directive

Introduction to Vue 3

Check out a free preview of the full Introduction to Vue 3 course

The "v-model Directive" Lesson is part of the full, Introduction to Vue 3 course featured in this preview video. Here's what you'd learn in this lesson:

Sarah explains that the V-model directive creates a relationship between the data in the component and a form input that allows the engineer to dynamically update values. A directive is a special token in the markup that tells the library to modify a DOM element. A Vue directive appears in the format of a prefixed HTML attribute. How to implement the v-model directive is also discussed in this segment.


Transcript from the "v-model Directive" Lesson

>> So v-model. This is one of my favorites. I love v-model. You got to have a favorite. It creates a relationship between the data and the instance or component in a form input so you can dynamically update values. A lot of what we do in front end development has to do with building forms.

[LAUGH] So this allows us to capture form inputs or capture differences between v-model inputs or text areas or things like that very quickly. It's called two way binding. So we're accepting user input and managing it in a responsible manner. We saw that message before that we're returning in data.

And we're saying this is a good place to type things. That's what we're starting with. So if I have a typical text area here, this is actually just plain HTML aside from the v-model. So we've got text area, class, rows, max length, that's a typical text area. And we have this v-model message that is establishing a relationship between what we're holding in data and what we're kind of gathering from the user in that text area.

And here we can output that onto the page in this p tag. So we've got these mustache templates where we can output it to the page. So here if we have this demo, where we say, hello there everyone, and I say I like tacos, and then you can see it directly outputting to the page very, very quickly.

I can type anything, and it's storing that data and outputting it as soon as I type. I didn't have to set state anywhere. I didn't have to gather anything about it. I didn't have to ping anything in order to find that information. So I just say, message, hello there everyone, and I mount the app.

And then in that text area, I established a relationship with message. So that means we can do really crazy things like I could do something like this, where I say write me, and then all of a sudden things scatter onto the page as I type things, or I can say, I like tacos, and I'm not afraid to show it.

And I can erase this and write it, and you already know how half of that demo was created. And in a little bit, you'll know how the other half is created, because we'll go into an animation section that shows how to do enter and leave animations as well.

Tada, cool. All right. So if we wanted to do something other than a textbox, let's say we wanted to do checkboxes or radio buttons or something like that, what we would do is we'd make Checkednames an array. And then we add that v-model to every input that we want to add to that array.

So I can say, John, Paul, George, Ringo, and take them off as well. And so if we go into our dev tools here, if we go into code pen, and then we can actually look at this in the view dev tools. We have our root, we have our Checkednames array.

And as I click on these, you can see things appending to that array. Cool. Now let's say you don't know how many of these items that you're gonna wanna put on the page. Let's say I'm a really big Beatles fan, and I'm super hard core, and I think, well, actually Pete Best was the fifth Beatle.

So then I could come in here, and I could add another value. And I could say Pete, and it's automatically appended, because what I did was I used that v4 that we saw earlier, and I'm looping through these options. And I'm establishing a relationship with those Checkednames. And we're about we're about to cover this bind right here.

So we're binding to the value, and then I'm able to just keep updating that. So that means I don't have to keep on copy, pasting inputs and labels and things like that. I can actually just dynamically create them from whatever happens on the server.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now