当前位置:网站首页>The principle of v-model
The principle of v-model
2022-07-31 10:37:00 【yibucuo】
v-model绑定input框
v-model 本质上不过是语法糖,可以用 v-model 指令在表单 <input>、<textarea> 及 <select> 元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.它负责监听用户的输入事件以更新数据,并对一些极端场景进行一些特殊处理.v-model 会忽略所有表单元素的 value、checked、selected 特性的初始值而总是将 Vue 实例的数据作为数据来源.你应该通过 JavaScript 在组件的 data 选项中声明初始值.v-model 在内部为不同的输入元素使用不同的属性并抛出不同的事件:text 和 textarea 元素使用 value 属性和 input 事件;checkbox 和 radio 使用 checked 属性和 change 事件;select 字段将 value 作为 prop 并将 change 作为事件.
<input v-model="searchText">
<input v-bind:value="searchText" v-on:input="searchText = $event.target.value" >
<!-- 自html5开始,input每次输入都会触发oninput事件, 所以输入时input的内容会绑定到searchText中,于是searchText的值就被改变; $event 指代当前触发的事件对象; $event.target 指代当前触发的事件对象的dom; $event.target.value 就是当前dom的value值; 在@input方法中,value => searchText; 在:value中,searchText => value; -->
v-model绑定input组件
v-model绑定一个value值,and customize oneinput事件v-on:input=“searchText = $event.target.value”
<KInput v-model="userInfo.username" placeholder="请输入用户名"></KInput>
组件内部,接受到value值,在用户触发input自带的input事件后,于是触发onInput,进一步this.$emit(‘input’),This triggers the above bindinginput事件,执行了searchText = $event.target.value
<template>
<div>
<input :type="type" :value="value" @input="onInput">
</div>
</template>
<script> export default {
props: {
value: {
type: String, default: '' }, type: {
type: String, default: 'text' } }, methods: {
onInput(e) {
// 这里就是v-model的原理,派发一个input事件即可 this.$emit('input', e.target.value) } }, } </script>
边栏推荐
- 7 天能找到 Go 工作吗?学学 Go 数组和指针试试
- 掌握SSR
- What is the encoding that starts with ?
- Build finished with errors/Executable Not Found
- Meikle Studio--Hongmeng 14-day development training notes (8)
- 半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!
- cocoaPods管理之后工程结构变化
- Sql optimization summary!detailed!(Required for the latest interview in 2021)
- 【JWT】JWT 整合
- Experience innovation and iteration through the development of a lucky draw applet
猜你喜欢
随机推荐
Sql optimization summary!detailed!(Required for the latest interview in 2021)
力扣shell刷题
“chmod 777-R 文件名”什么意思?
Qt compile error: C2228: '.key' must have class/struct/union on the left
学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架
The fifth chapter
Make your own dataset in FCN and train it
Creation of doubly linked list
如何优雅的停止一个线程?
Open Kylin openKylin automation developer platform officially released
Deletion of the sequence table
Dart Log工具类
【LeetCode】36.有效的数独
csdn文件导出为pdf
乐观锁和悲观锁
nodeJs--querystring模块
细讲DDD领域驱动设计
P5231 [JSOI2012]玄武密码(SAM 经典运用)
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
What is the encoding that starts with ?









