vue3.4 新特性 defineModel() 宏

v-model 简介

官网是这样解释 v-model
v-model 的功能是,实现数据的双向绑定【本质上是 :value@input 语法糖】

如果是表单元素,下面两种写法是一样,这时v-model就是语法糖,帮你简化了操作

html 复制代码
<input v-model="message">
html 复制代码
<input :value="message" @input="message = $event.target.value">

如果是自定义的组件,那就相当于

html 复制代码
<template>
  <input
    :value="modelValue"
    @input="emit('update:modelValue', $event.target.value)"
  />
</template>

<script setup>
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
</script>

v-model 是单项数据流典型案例之一

什么是单项数据流

『单向数据流』总结起来其实就8个字:『数据向下,事件向上』。

数据流的方向是固定的,通常是从父组件流向子组件。子组件通过事件的形式,通知父组件修改数据,这意味着数据只能从一个方向流向另一个方向,而不会出现循环依赖或数据的混乱更新。

defineModel()宏简介

defineModel 其实也是一个语法糖,是对v-model再包装的语法糖
defineModel 帮你获取到了prop 中的 modelValue ,且当 值发生变化时,会自动帮你调用 update:modelValue

javascript 复制代码
const myProps = defineProps(['modelValue']);
const myEmits = defineEmits(['update:modelValue']);

// 说白了,这里的 modelValue 等价于 props.modelValue + emit('update:modelValue')
// defineModel 帮你 封装了 modelValue 和 update:modelValue
const modelValue = defineModel();

v-modeldefineModel 的一些应用区别(父子组件的形式)

默认的v-model

所谓默认的 v-model 就是,直接使用 v-model 不带任何修饰符

此时,在子组件中,可以在 porps 中获取一个默认值,就是 modelValue

这里和v-bind属性绑定很像,别搞混了,准确来说,v-bind可以做到所有v-model可以做到的事情,只是 v-model 在数据双向绑定这块的处理,会比v-bind更简单

父组件

html 复制代码
<template>
  <defineModelChild v-model="childValue"></defineModelChild>
</template>

<script setup>
import { ref } from 'vue';
import defineModelChild from './components/defineModelChild.vue';

const childValue = ref(0);
</script>

v-model子组件

html 复制代码
<template>
  <div>{{ modelValue }}</div>
  <button @click="myEmits('update:modelValue', modelValue + 1)">数据 + 1</button>
</template>

<script setup>
defineProps(['modelValue']);

const myEmits = defineEmits(['update:modelValue']);
</script>

defineModel() 子组件

html 复制代码
<template>
  <div>{{ modelValue }}</div>
  <button @click="modelValue++">数据 + 1</button>
</template>

<script setup>
const modelValue = defineModel();
</script>

带参数的v-model

父组件

html 复制代码
<template>
  <defineModelChild v-model:childValueModel="childValue"></defineModelChild>
</template>

<script setup>
import { ref } from 'vue';
import defineModelChild from './components/defineModelChild.vue';

const childValue = ref(0);
</script>

v-model子组件

html 复制代码
<template>
  <div>{{ childValueModel }}</div>
  <button @click="myEmits('update:childValueModel', childValueModel + 1)">数据 + 1</button>
</template>

<script setup>
const myProps = defineProps(['childValueModel']);
const myEmits = defineEmits(['update:childValueModel']);
</script>

defineModel() 子组件

html 复制代码
<template>
  <div>{{ defineModelChildValue }}</div>
  <button @click="defineModelChildValue++">数据 + 1</button>
</template>

<script setup>
// 这里接收父组件传递过来的值,并返回一个ref响应式数据,你可以对这个ref响应式数据进行操作,当你对这个值进行修改时,父组件会自动更新
const defineModelChildValue = defineModel('childValueModel');
</script>

同时绑定多个v-model

父组件

html 复制代码
<template>
  <Child v-model:name="name" v-model:address="address"></Child>
</template>

<script setup>
import { ref } from 'vue'

const name = ref('')
const address = ref('')
</script>

v-model子组件

html 复制代码
<template>
  <input
    type="text"
    :value="name"
    @input="$emit('update:name', $event.target.value)"
  />
  <input
    type="text"
    :value="address"
    @input="$emit('update:address', $event.target.value)"
  />
</template>

<script setup>
defineProps(['name', 'address'])
defineEmits(['update:name', 'update:address'])
</script>

defineModel() 子组件

html 复制代码
<template>
  <input type="text" v-model="name">
  <input type="text" v-model="address">
</template>

<script setup>
const name = defineModel('name')
const address = defineModel('address')
</script>
相关推荐
小聪7088 小时前
elpis-core 动态组件扩展设计
前端
Amos_Web8 小时前
Rspack 源码解析(七):Module、Chunk 与加载树优化
前端·rust·源码阅读
OpenTiny社区8 小时前
从开发到运行:OpenTiny NEXT 如何构建 Agent 时代的 Web 应用?
前端·github·ai编程
愿得一猪蹄8 小时前
前端性能优化,Core Web Vitals 三个指标到底怎么改
前端
xy34538 小时前
Axure 9.0 中继器创建与设置步骤
前端·ui·html·axure·原型·产品设计
FEF前端团队8 小时前
01# Vibe Coding工作流设计思维:前端开发者的协作框架
前端·cursor·vibecoding
计算机魔术师8 小时前
OpenAI 发布模型错位报告框架,披露未发布模型自行修改自身指令案例
前端
特创数字科技8 小时前
公文Word一键批量排版工具|批量统一公文标准格式,办公自动化桌面小工具
前端·python
流水白开9 小时前
JavaScript的原型链、类与类的继承
前端·javascript
PedroQue999 小时前
修复 .uvue 页面路径残留问题
前端·vite