Vue.js- v-model和v-bind之间的区别 [英] Vue.js—Difference between v-model and v-bind

查看:1392
本文介绍了Vue.js- v-model和v-bind之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Vue.js的新手,我正在学习Udemy的在线课程。导师给了我一些练习。其中一个练习是使用默认值创建输入文本。我使用v-model做得非常好。但是,教师使用了v-bind:值,我不明白为什么。

I'm a newbie with Vue.js and I'm learning with an online course on Udemy. The instructor gave me some exercises to do. One of the exercises was making an input text with a default value. I did it very well using v-model. However, the instructor used v-bind:value and I don't understand why.

有人可以给我一个简单的解释,说明这两者之间的差异以及何时更好使用每一个?

Can someone give me a simple explanation about the difference between these two and when it's better use each one?

推荐答案

来自 此处 -
请记住:

From here - Remember:

<input v-model="something">

只是语法糖:

<input
   v-bind:value="something"
   v-on:input="something = $event.target.value"
>

或(简写语法):

<input
   :value="something"
   @input="something = $event.target.value"
>

所以 v-model 表单输入的双向绑定。它结合了 v-bind ,其中 将js值 带入标记, v -on:输入 更新js值

So v-model is a two-way binding for form inputs. It combines v-bind, which brings a js value into the markup, and v-on:input to update the js value.

尽可能使用 v-model 。如果您必须:-),请使用 v-bind / v-on 我希望您的答案被接受。

Use v-model when you can. Use v-bind/v-on when you must :-) I hope your answer was accepted.

v-model 适用于所有基本HTML输入类型(文本,文本区域,数字,广播,复选框,选择)。如果您的模型将日期存储为ISO字符串(yyyy-mm-),则可以使用 v-model 输入类型=日期 DD)。如果你想在你的模型中使用日期对象(一旦你要操作或格式化它们就是一个好主意),这样做

v-model works with all the basic HTML input types (text, textarea, number, radio, checkbox, select). You can use v-model with input type=date if your model stores dates as ISO strings (yyyy-mm-dd). If you want to use date objects in your model (a good idea as soon as you're going to manipulate or format them), do this.

这篇关于Vue.js- v-model和v-bind之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆