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>
相关推荐
少年姜太公21 分钟前
什么?还不知道git cherry pick?
前端·javascript·git
白兰地空瓶2 小时前
🏒 前端 AI 应用实战:用 Vue3 + Coze,把宠物一键变成冰球运动员!
前端·vue.js·coze
Liu.7743 小时前
vue3使用vue3-print-nb打印
前端·javascript·vue.js
松涛和鸣4 小时前
Linux Makefile : From Basic Syntax to Multi-File Project Compilation
linux·运维·服务器·前端·windows·哈希算法
dly_blog4 小时前
Vue 逻辑复用的多种方案对比!
前端·javascript·vue.js
万少4 小时前
HarmonyOS6 接入分享,原来也是三分钟的事情
前端·harmonyos
烛阴4 小时前
C# 正则表达式:量词与锚点——从“.*”到精确匹配
前端·正则表达式·c#
wyzqhhhh5 小时前
京东啊啊啊啊啊
开发语言·前端·javascript
JIngJaneIL5 小时前
基于java+ vue助农电商系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
q_19132846955 小时前
基于Springboot+MySQL+RuoYi的会议室预约管理系统
java·vue.js·spring boot·后端·mysql·若依·计算机毕业设计