vue----v-model

v-model="state" 本质是 Vue 提供的一个语法糖(syntactic sugar),用于简化「父子组件之间的双向绑定」。

一、基础等价转换

html 复制代码
<Child v-model="state" />

等价于:

html 复制代码
<Child 
  :modelValue="state" 
  @update:modelValue="val => state = val" 
/>

二、子组件实现示例

  1. 子组件(Child.vue)
html 复制代码
<template>
  <input 
    :value="modelValue" 
    @input="handleInput"
  />
</template>

<script setup>
const props = defineProps({
  modelValue: String
})

const emit = defineEmits(['update:modelValue'])

const handleInput = (e) => {
  emit('update:modelValue', e.target.value)
}
</script>
  1. 父组件
html 复制代码
<template>
  <Child v-model="state" />
  <p>{{ state }}</p>
</template>

<script setup>
import { ref } from 'vue'
import Child from './Child.vue'

const state = ref('hello')
</script>
相关推荐
Csvn7 分钟前
构建工具
前端
FRing0337 分钟前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
Java小卷1 小时前
JuanForm vs FormCreate!Vue3 表单引擎联动配置 PK!
vue.js·低代码
kyriewen3 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒3 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫4 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库
jerrywus4 小时前
我用 GPT-6.1-sol 做了台会讲解的 3D 售货机:60 罐饮料,一口没喝上
前端·agent·claude
孟陬5 小时前
Foobar 技术周刊#159:当看不懂代码时,如何高效排障调试。脏页陷阱:一条只读查询,写入数 TB 数据。推测解码:大模型加速利器
前端·人工智能·后端
Zhao-Jichao5 小时前
AI Prompting for Everyone 课程笔记 - P17 图像生成 Imaging Generation
前端
Highcharts.js6 小时前
智能数据分析组件Orbit 正式发布与快速上手体验教程
前端·人工智能·信息可视化·数据分析系统·highcharts·orbit·智能可视化