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>
相关推荐
一袋米扛几楼989 分钟前
【网络安全】SIEM -Security Information and Event Management 工具是什么?
前端·安全·web安全
小陈工20 分钟前
2026年4月7日技术资讯洞察:下一代数据库融合、AI基础设施竞赛与异步编程实战
开发语言·前端·数据库·人工智能·python
Cobyte29 分钟前
3.响应式系统基础:从发布订阅模式的角度理解 Vue2 的数据响应式原理
前端·javascript·vue.js
竹林81832 分钟前
从零到一:在React前端中集成The Graph查询Uniswap V3池数据实战
前端·javascript
Mintopia40 分钟前
别再迷信"优化":大多数性能问题根本不在代码里
前端
倾颜40 分钟前
接入 MCP,不一定要先平台化:一次 AI Runtime 的实战取舍
前端·后端·mcp
军军君0142 分钟前
Three.js基础功能学习十八:智能黑板实现实例五
前端·javascript·vue.js·3d·typescript·前端框架·threejs
恋猫de小郭43 分钟前
Android 上为什么主题字体对 Flutter 不生效,对 Compose 生效?Flutter 中文字体问题修复
android·前端·flutter
Moment44 分钟前
AI全栈入门指南:一文搞清楚NestJs 中的 Controller 和路由
前端·javascript·后端
禅思院1 小时前
前端架构演进:基于AST的常量模块自动化迁移实践
前端·vue.js·前端框架