当前位置: 首页 > news >正文

vue3学习记录-v-model

vue3学习记录-v-model

  • 1.疑问
  • 2 用法
    • 2.1底层原理
    • 2.2 多个v-model
    • 2.3 defineModel()
    • 2.4 自定义修饰符

1.疑问

像那种原生标签,例如input、textarea 及 select上使用v-model,会创建双向数据绑定。那像那种自定义的组件v-model是怎么实现双向绑定的呢。

2 用法

2.1底层原理

一个名为 modelValue 的 prop,本地 ref 的值与其同步;
一个名为 update:modelValue 的事件,当本地 ref 的值发生变更时触发。

//父组件
<script setup>
import A from './components/A.vue';
import { ref } from 'vue';
const countModel = ref(0);
</script>
<template><div class="container"><p>countModel:{{ countModel }}</p><A v-model="countModel"></A><B v-model="countModel1"></B></div>
</template>//子组件
<template>
<input type="text" :value="modelValue" @input="inputChange">
</template>
<script setup>
defineProps({modelValue:String
})
const inputChange =(e) =>{console.log(e.target.value)emit('update:modelValue',e.target.value)
}
const emit = defineEmits(['update:modelValue'])
</script>

父组件中的 v-model=“foo” 将被编译为:

<A
:modelValue=“countModel”
@update:modelValue=“$event => (countModel= $event)”
/>

2.2 多个v-model

Vue 3 允许在一个组件上使用多个 v-model

<!-- CustomForm.vue -->
<script setup>
defineProps(['firstName', 'lastName'])
defineEmits(['update:firstName', 'update:lastName'])
</script><template><input :value="firstName"@input="$emit('update:firstName', $event.target.value)"/><input :value="lastName"@input="$emit('update:lastName', $event.target.value)"/>
</template>//父组件
<script setup>
import CustomForm from './components/CustomForm.vue';
import { ref } from 'vue';
const firstName = ref('')
const lastName = ref('')
</script>
<template><div class="container"><CustomFormv-model:firstName="firstName"v-model:lastName="lastName"/><p>全名:{{ firstName }} {{ lastName }}</p></div>
</template>

2.3 defineModel()

从 Vue 3.4 开始,推荐的实现方式是使用 defineModel() 宏:

<!-- Child.vue -->
<script setup>
const model = defineModel()function update() {model.value++
}
</script><template><div><div>Parent bound v-model is: {{ model }}</div><input type="text" v-model="model"><button @click="update">Increment</button></div>
</template>

defineModel的写法input绑定值用的是v-model,单个或则多个v-model和底层原理类似的写法

2.4 自定义修饰符

Vue 3 还允许我们为 v-model 创建自定义修饰符。自定义一个capitalize实现首字母大写.

<script setup>
import B from './components/B.vue';
import { ref } from 'vue';
const countModel = ref('');
</script><template><div class="container"><B v-model.capitalize="countModel"></B></div>
</template><template>
<input type="text" :value="modelValue" @input="inputChange">
</template><script setup>
import { ref, reactive} from 'vue'
const props = defineProps({modelValue:String,modelModifiers:{default: ()=> {}}
})
const inputChange =(e) =>{let value = e.target.valueif (props.modelModifiers.capitalize) {value = value.charAt(0).toUpperCase() + value.slice(1)}emit('update:modelValue',value)
}
const emit = defineEmits(['update:modelValue'])
</script>

3.4之后的写法

<!-- Child.vue -->
<script setup>
const [model, modifiers] = defineModel({set(val){if(modifiers.capitalize){return val.charAt(0).toUpperCase() + val.slice(1)}return val}
})
</script><template><div>Parent bound v-model is: {{ model }}</div><input type="text" v-model="model">
</template>

http://www.mrgr.cn/news/56250.html

相关文章:

  • 十一、【智能体】一键生成文章!秒懂!一篇搞定智能体工作流核心操作,轻松上手!效率飙升N倍!
  • Go 语言中格式化动词
  • 深度学习-机器学习与传统编程区别
  • msfvenom生成木马-windows
  • classnames 使用
  • 2024年前端开发者必备的20个神器 - 提升效率的终极指南
  • Numpy基础02
  • 浏览器控制的无线开关
  • 【03】RabbitMQ核心功能扩展
  • LeetCode718:最长重复子数组
  • [DB] NSM
  • 在线教育(培训+考试)/企业培训-企业培训平台-企业培训平台系统-企业内部培训系统-在线教育-Java语言开发
  • 「AIGC」n8n AI Agent开源的工作流自动化工具
  • php基础:数据类型、常量、字符串
  • 【内信互联】私有化安全性企业远程运维办公解决方案
  • Redis-04 Redis管道
  • 《黑神话:悟空》:又是这只跨界的猴子,诠释了传承与创新的关系
  • 【1】从零开始学习目标检测:YOLO算法详解
  • 1024程序员节:永无bug
  • 《远程桌面方案全析:开启高效远程协作新时代》
  • 美容师流失率高怎么办?怎样可以降低美容师的流失率?博弈美业门店收银系统管理系统分享
  • [LeetCode] 50. Pow(x, n)
  • 基于STM32的多功能MP3播放器
  • 数字信号处理实验简介
  • 流程引擎在企业管理中的关键作用
  • 双十一开启极速达夜派;黑神话获泰国年度最佳游戏;AI 模型可帮助识别 17000 多种疾病的候选药物....| 网易数智日报