Through the previous learning, we can successfully render the data source into the HTML DOM element in Vue, but many times we hope to control the data source. That is, data binding and communication with it. Simply put, how to implement two-way binding of data in Vue. This scenario is usually in the form operation scenario. This can be achieved in Vue using the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
directive. However, in this article we are only here to learn the use of the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
directive, but we will not explore the principle of two-way binding of Vue data. If you are interested in the principle, you can read this article.
In Vue, use {{}}
or v-text
, you can render data from the data source into DOM elements. (Learning video sharing: vue video tutorial)
For example:
<!-- Template --> <h1>{{ message }}</h1> let app = new Vue({ el: '#app', data: { message: 'Hello W3cplus! (^_^)' } })
Based on the above example, let’s modify it Requirements, we want to modify {{message}}
through an input of <input>
. At this time we need to use Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
. Let’s look at the example first, and then talk about Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
. Add an input
based on the above example. The modified template code is as follows:
<!-- Template --> <div id="app"> <div> <input type="text" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="message" placeholder="Hello W3cplus!(^_^)" /> </div> <h1>{{ message }}</h1> </div>
As can be seen from the effect, modify The
value value of input
and the content of the corresponding h1
element are also modified. This effect is the effect of two-way data binding.
The key point here is the use of the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
command. In Vue, you can use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
directive to create two-way data binding on a form control element. It automatically chooses the correct method to update the element based on the control type. Although somewhat magical, Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
is essentially just syntactic sugar. It is responsible for listening to user input events to update data, and specially handle some extreme cases.
Note:
Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
will ignore thevalue
,checked
,selected
attributes of all form elements initial value. Because it will select Vue instance data as the specific value. You should declare the initial value via JavaScript in the component'sdata
option.
In Vue, Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
is mainly used for form controls. So next, let's take a look at how Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
is used on common form controls.
The example demonstrated previously is actually the effect of a single-line text input box. input
binds the value of Vue’s data source through Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
. When the input
input value changes, the data will also change accordingly, and the interpolation of element rendering will also change accordingly.
The data
here can be object
or function
, but the data
of the component can only be function
, this is because each component has its own data
, rather than a shared relationship. However, today we will not look at the two-way binding of data in components, but simply look at the form controls. Without digressing, let’s return to the input
box. We usually write code in the template like the following:
<!-- Template --> <div id="app"> <div> <input type="text" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="message" placeholder="Hello W3cplus!(^_^)" /> </div> <h1>{{ message }}</h1> </div>
Similarly, in the Vue instance, you need to specify data
, our example refers to message
:
let app = new Vue({ el: '#app', data: { message: 'Hello W3cplus! (^_^)' } })
Refresh your browser. When you modify the input value of input
, you can see the corresponding synchronous changes in the content of the h1
element. The effect is as shown below :
Multi-line text areatextarea
and input
Very similar. For example, change the input
in the above example to the textarea
form element, and similarly bind Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="message"
to textarea
element. The template code is as follows:
<!-- Template --> <div id="app"> <div> <textarea Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="message" cols="30" rows="5"></textarea> </div> <h2>{{ message }}</h2> </div>
When we modify the content in textarea
, the corresponding h2
will also change:
小结一下:当使用文本input
(包括email
,number
等)或textarea
时,Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="varName"
等价于:value="varName" @input="e => varName = e.target.value"
。这意味着每次输入完成后的varName
将被更新为输入的值,然后输入的值被设置为varName
。正常的select
元素也会像这样,尽管multiple
多项选择有所不同。
在文本区域插值 (
<textarea></textarea>
) 并不会生效,应用Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
来代替。
在Vue中的复选框通过Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
绑定数据源,并不和我们Web表单中的复选框一样。比如下面的示例:
<!-- Template --> <div> <input type="checkbox" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="checked" id="checkbox" /> <label for="checkbox">{{checked}}</label> </div> // JavaScript let app = new Vue({ data: { checked: false } })
刷新浏览器,当复选框选中的时候,checked
的值false
就变成了true
。如下图所示:
不知道你跟我是不是同样的好奇,如果input
复选框设置了checked
(默认选中),会不会刷新浏览器,checked
的值会不会变成true
。从实战来看,是不会有变化的,只有选中之后,false
才会变成true
。另外再试一个效果,如果checked
默认值不是false
,是任何字符串,看看是什么效果?
是不是很神奇,虽然默认选中,并且点击之后就在false
和true
之间切换。
上面的示例,咱们只使用单个复选框,从效果上告诉我们Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
会将其视为布尔值,并且会忽略该value
。而且:
<input type="checkbox" value="foo" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="isChecked" />
将和下面的代码相同:
<input type="checkbox" value="foo" :checked="!!isChecked" @change="e => isChecked = e.target.checked" />
如果想要它是非布尔值,可以使用true-value
和false-value
属性,它控制当选择复选框时,模型将被设置成什么值。
<input type="checkbox" value="foo" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="isChecked" true-value="1" false-value="0">
与以下代码相同
<input type="checkbox" value="foo" :checked="isChecked =='1'" @change="e => isChecked = e.target.checked?'1':'0'">
单一复选框的情况差不多就是这样。如果有多个复选框共享一个数据源(Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
指定的值),那么这些复选框将填充一个数组,其值为所有勾选的复选框,但一定要在数据源中指定数据是一个数组类型,否则会产生一些奇怪的现象。来看一个多选项的示例:
<!-- Template --> <div> <input type="checkbox" id="jack" value="Jack" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="John" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="checkedNames"> <label for="john">John</label> <input type="checkbox" id="mike" value="Mike" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="checkedNames"> <label for="mike">Mike</label> </div> <div>选中的值:{{checkedNames}}</div> // JavaScript var app = new Vue({ el: '#app', data: { checkedNames: [] } })
效果如下:
当我们使用多个复选框时,true-value
和false-value
属性不再有效。同时在模板中像上面示例使用方式,很难保证一致性,所以最好的方式是将一些逻辑移到组件的方法上。不过我们这节并不介绍怎么应用到组件中,因为我们还没有学习Vue的组件怎么创建。如果你感兴趣的话,可以观注后续更新的内容,我们将会花一节内容来看看怎么完成自定义的复选框组件。
Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
在单选按钮上的使用,咱们先来上实例代码吧:
<!-- Template --> <div id="app"> <div> <input type="radio" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="selected" value="CSS" id="css"/> <label for="css">CSS</label> <input type="radio" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="selected" value="HTML" id="html"/> <label for="html">HTML</label> <input type="radio" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="selected" value="JavaScript" id="javascript"/> <label for="javascript">JavaScript</label> <br> <div>你最喜欢的是:{{selected}}</div> </div> </div> // JavaScript var app = new Vue({ el: '#app', data: { selected: null } })
更新浏览器,看到的效果如下:
Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
在选择框上的使用,可以运用在单项选择框和多项选择框,不同的是,多项选择框在数据源中应该是一个数组。比如下面的示例:
<!-- Template --> <div id="app"> <h1>选择框</h1> <div> <select Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="selected"> <option disabled value="">请选择</option> <option>CSS</option> <option>HTML</option> <option>JavaScript</option> </select> <span>请选择: {{ selected }}</span> </div> <div> <select Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="multipleSelected" multiple> <option>CSS</option> <option>HTML</option> <option>JavaScript</option> <option>PHP</option> </select> <span>请选择:{{multipleSelected}}</span> </div> </div> // JavaScript var app = new Vue({ el: '#app', data: { selected: null, multipleSelected: [] } })
效果如下:
如果 Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
表达初始的值不匹配任何的选项,<select>
元素就会以”未选中”
的状态渲染。在 iOS 中,这会使用户无法选择第一个选项,因为这样的情况下,iOS 不会引发 change
事件。因此,像以上提供 disabled
选项是建议的做法。
对于选择框,如果是动态选择框的话,我们可以利用前面学习的v-for
指令来帮助我们:
<!-- Template --> <div id="app"> <select Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="selected"> <option v-for="option in options" v-bind:value="option.value"> {{ option.text }} </option> </select> <div>你选择了: {{ selected }}</div> </div> // JavaScript let app = new Vue({ el: '#pp', data: { selected: '请选择', options: [ { text: 'One', value: 'CSS' }, { text: 'Two', value: 'HTML' }, { text: 'Three', value: 'JavaScript' } ] } })
效果是什么样呢?自己动手写一下。这里我们用到了Vue的一个新指令,那就是v-bind
指令,他有什么功能或特性呢?咱们后续会学习。
上面向大家展示了Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
指令在常见的表单控件上的使用情况。建议大家自己动手撸一下代码,体会将会更深。
Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
的修饰符Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
还有一些修饰符的功能,主要有.lazy
、number
和.trim
。其主要功能是:
.lazy
:默认情况下,Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
在input
事件中同步输入框的值与数据,但添加了.lazy
修饰符之后,从而转变为在change
事件中同步。简单点说就是延迟了.number
:如果想自动将用户的输入值转换为Number
类型(如果原值的转换结果为NaN
则返回原值),可以添加.number
修饰符给Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
来处理输入值.trim
:如果要自动过滤用户输入的首尾空格,可以添加.trim
修饰符给Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
上过滤输入路们来看一个示例:
<!-- Template --> <div id="app"> <ul> <li> <label>不带任何修饰符:</label> <input type="text" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue="message" placeholder="Hello W3cplus!(^_^)" /> <span>{{message}}</span> </li> <li> <label>带.lazy修饰符:</label> <input type="text" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue.lazy="message" placeholder="Hello W3cplus!(^_^)" /> <span>{{message}}</span> </li> <li> <label>带.number修饰符:</label> <input type="text" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue.number="message" placeholder="Hello W3cplus!(^_^)" /> <span>{{message}}</span> </li> <li> <label>带.trim修饰符:</label> <input type="text" Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue.trim="message" placeholder="Hello W3cplus!(^_^)" /> <span>{{message}}</span> </li> </ul> </div> // JavaScript var app = new Vue({ el: '#app', data: { message: 'Hello W3cplus!(^_^)', } })
刷新页面看效果:
从上在的效果可以看出:
input
的值,message
立马变同步input
的输入值.lazy
修饰符: 修改input
的值,message
并不会立马同步input
的输入值,只有当input
失去焦点时,message
才会同步input
的输入值.number
修饰符: 当输入框的值,以数字加其他字符组合的内容,会自动去除其他的字符,只留数字;如果是其他字符加数字组合的内容,并不会删除其他字符,只留数字。一般带.number
修饰符的input
控制配合type="number"
配合使用.trim
修饰符: input
输入框开始或末尾有空字符,将会自动删除空字符,如果空字符在其他字符中间,则不会删除空字符这篇文章主要学习了Vue中的Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
的简单功能,就是和表单控件实现双向数据绑定。其实很多时候HTML内建的input
类型有时不能满足我们的需求。这个时候有需要通过Vue的组件系统来创建自定义行为而且可复用的input
类型。这些input
类型甚至可以和Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
一起使用。这就涉及到了Vue的组件。那么Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue
怎么和组件一起使用呢?我们后续将会学习这部分,因为学习这部分内容,需要对组件有一定的了解。
原文地址:https://www.w3cplus.com/vue/Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the Detailed examples of how to use the v-model directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue directive in Vue.html
The above is the detailed content of Detailed examples of how to use the v-model directive in Vue. For more information, please follow other related articles on the PHP Chinese website!