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>
相关推荐
guslegend1 小时前
脚手架原理与本地调试:从 bin 软链接到 npm link
前端·npm·node.js·脚手架·前端工程化
小宋10211 小时前
A2UI从零实战:Agent安全生成交互界面与事件回传
javascript·人工智能·安全·交互
大包子2 小时前
structuredClone vs lodash.cloneDeep:核心差异与选型指南
javascript·vue.js
海码事务所2 小时前
Google Play 新个人开发者账号上架指南:12 人连续 14 天封闭测试怎么做?
前端
田威AI2 小时前
图片内文字翻译的规格:输入输出、保真、自动化、时间与费用
前端·计算机视觉
不可能片场2 小时前
命令行中文变问号 我用环境变量救了场
前端·electron
lerhxx2 小时前
AI 应用中的上下文管理 —— 从"上下文窗口"到"分层压缩"的工程实践
前端·javascript
骑着蜗牛撵大象3272 小时前
多 Agent 分治协作:MapReduce 模式下的并行扇出与结果归并
前端
用户55318297322 小时前
Flutter iOS 热更新深入 Dart VM:读懂函数入口、解释执行与调用桥接
前端
前端snow3 小时前
为什么大厂要用Postgres + mogodb的框架?
前端