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>
相关推荐
We་ct2 小时前
深度剖析浏览器跨域问题
开发语言·前端·浏览器·跨域·cors·同源·浏览器跨域
weixin_427771613 小时前
前端调试隐藏元素
前端
threelab3 小时前
Three.js 代码云效果 | 三维可视化 / AI 提示词
开发语言·javascript·人工智能
爱上好庆祝4 小时前
学习js的第五天
前端·css·学习·html·css3·js
C澒4 小时前
IntelliPro 产研协作平台:基于 AI Agent 的低代码智能化配置方案设计与实现
前端·低代码·ai编程
一袋米扛几楼984 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
网络点点滴4 小时前
前端与后端的区别与联系
前端
yqcoder4 小时前
JavaScript 柯里化:把“大餐”拆成“小炒”的艺术
开发语言·javascript·ecmascript
每天吃饭的羊5 小时前
JSZip的使用
开发语言·javascript
EnCi Zheng5 小时前
M5-markconv自定义CSS样式指南 [特殊字符]
前端·css·python