vue3的v-model

基本用法

v-model可以在组件上实现双向绑定

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

vue 复制代码
<script setup>
const model = defineModel()
</script>

<template>
  <span>My input</span> <input v-model="model">
</template>

在子组件中使用defineModel声明一个model变量,在模板中使用v-model绑定这个变量

vue 复制代码
// App.vue
<script setup>
import Child from './Child.vue'
import { ref } from 'vue'

const msg = ref('Hello World!')
</script>

<template>
  <h1>{{ msg }}</h1>
  <Child v-model="msg" />
</template>

在父组件声明一个响应变量msg,再用v-model绑定msg,这样msg的值传给了子组件

效果:在子组件中修改model的值,父组件的msg也随之变化。

底层原理

vue 复制代码
<script setup>
const model = defineModel()
</script>

<template>
  <span>My input</span> <input v-model="model">
</template>

可以拆解成由defineProps()和defineEmit()的组合

vue 复制代码
<!-- Child.vue -->
<script setup>
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
</script>

<template>
  <input
    :value="props.modelValue"
    @input="emit('update:modelValue', $event.target.value)"
  />
</template>
vue 复制代码
// App.vue
<script setup>
import Child from './Child.vue'
import { ref } from 'vue'

const msg = ref('Hello World!')
</script>

<template>
  <h1>{{ msg }}</h1>
  <Child v-model="msg" />
</template>

被拆解成

vue 复制代码
<!-- APP.vue -->
<Child
  :modelValue="msg"
  @update:modelValue="$event => (msg = $event)"
/>

在子组件中用props.modelValue接受父组件传来的msg的值,在模板中用:value进行响应式渲染。

当模板中input的value被修改的时候,使用emit中update:modelValue向父组件传value的值,然后父组件的msg的值被修改成value的值。

v-model的参数

在组件上的v-model可以接受一个参数:

vue 复制代码
// App.vue
<script setup>
import Child from './Child.vue'
import { ref } from 'vue'

const msg = ref('Hello World!')
</script>

<template>
  <h1>{{ msg }}</h1>
  <Child v-model:title="msg" />
</template>

在子组件中,我们可以通过将字符串作为第一个参数传递给 defineModel() 来支持相应的参数。

vue 复制代码
<script setup>
const title = defineModel('title')
</script>

<template>
  <span>My input</span> <input v-model="model">
</template>

如果需要额外的 prop 选项,应该在 model 名称之后传递

js 复制代码
const title = defineModel('title', { required: true })
相关推荐
快起来别睡了4 分钟前
用这 9 个 API,我把页面性能干到了 90+
前端
芒果茶叶16 分钟前
深入浅出requestAnimationFrame
前端·javascript·html
歪歪10018 分钟前
在哪些场景下适合使用 v-model 机制?
服务器·前端·javascript·servlet·前端框架·js
亲爱的马哥20 分钟前
再见,TDuckX3.0 结束了
前端·后端·github
KrystalMeiling33 分钟前
讲解react的useEffect用法,被监听字段的注意事项
前端
渣哥37 分钟前
Spring 创建 Bean 的多种方式对比与最佳实践
前端·javascript·面试
SanOrintea38 分钟前
electron中进程线程之间通信方式
服务器·javascript·electron
Copper peas41 分钟前
axios使用过程
前端·javascript·vue.js
云鹤_1 小时前
【Amis源码阅读】如何将json配置渲染成页面?
前端·低代码
逛逛GitHub1 小时前
飞书多维表格 + 即梦 4.0,打造你的 AI 生图游乐场。
前端·github