Vue3中的v-model

1、v-model

Vue2

Vue2中得 v-model 默认解析成 :value 与 @input

原理实现

:

<template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld :value="count" @input="count = $event"></HelloWorld></div>
</template><script>
import HelloWorld from "./components/HelloWorld.vue";export default {name: "App",components: {HelloWorld,},data() {return { count: 0 };}
};
</script><style></style>

:

<template><div>{{ value }}<button @click="$emit('input', value + 1)">+1</button></div>
</template><script>
export default {name: "HelloWorld",props: {value: Number,},
};
</script>
<style scoped></style>

语法糖实现

:

<template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld v-model="count" /></div>
</template><script>
import HelloWorld from "./components/HelloWorld.vue";export default {name: "App",components: {HelloWorld,},data() {return { count: 0 };}
};
</script><style></style>

:

<template><div>{{ value }}<button @click="$emit('input', value + 1)">+1</button></div>
</template><script>
export default {name: "HelloWorld",props: {value: Number,},
};
</script>
<style scoped></style>

Vue3

Vue3中得 v-model 默认解析成 :modelValue 与 @update:modelValue

原理实现

:

<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script><template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld:modelValue="count"@update:modelValue="count = $event"></HelloWorld></div>
</template><style lang="scss" scoped></style>

:

<script setup lang="ts">
defineProps<{modelValue: number
}>()
defineEmits<{(e: 'update:modelValue', newCount: number): void
}>()
</script><template><div>{{ modelValue }}<button @click="$emit('update:modelValue', modelValue + 1)">+1</button></div>
</template><style lang="scss" scoped></style>

语法糖实现

<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script><template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld v-model="count"></HelloWorld></div>
</template><style lang="scss" scoped></style>

:

<script setup lang="ts">
defineProps<{modelValue: number
}>()
defineEmits<{(e: 'update:modelValue', newCount: number): void
}>()
</script><template><div>{{ modelValue }}<button @click="$emit('update:modelValue', modelValue + 1)">+1</button></div>
</template><style lang="scss" scoped></style>

2、 .sync

Vue2

Vue2中得 :attr.sync 默认解析成 :attr 与 @update:attr

原理实现

<template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld :count="count" @setCount="count = $event" /></div>
</template><script>
import HelloWorld from "./components/HelloWorld.vue";export default {name: "App",components: {HelloWorld,},data() {return { count: 0 };}
};
</script><style></style>

<template><div class="hello">{{ count }}<button @click="$emit('setCount', count + 1)">+1</button></div>
</template><script>
export default {name: "HelloWorld",props: {count: Number,},
};
</script>
<style scoped></style>

语法糖实现

<template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld :count.sync="count" /></div>
</template><script>
import HelloWorld from "./components/HelloWorld.vue";export default {name: "App",components: {HelloWorld,},data() {return { count: 0 };}
};
</script><style></style>

<template><div class="hello">{{ count }}<button @click="$emit('update:count', count + 1)">+1</button></div>
</template><script>
export default {name: "HelloWorld",props: {count: Number,},
};
</script>
<style scoped></style>

Vue3

Vue3中得 v-model:attr 默认解析成 :attr 与 @update:attr

原理实现

:

<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script><template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld :count="count" @setCount="count = $event"></HelloWorld></div>
</template><style lang="scss" scoped></style>

:

<script setup lang="ts">
defineProps<{count: number
}>()
defineEmits<{(e: 'setCount', newCount: number): void
}>()
</script><template><div>{{ count }}<button @click="$emit('setCount', count + 1)">+1</button></div>
</template><style lang="scss" scoped></style>

语法糖实现

:

<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script><template><div>{{ count }}<button @click="count++">+1</button><hr /><HelloWorld v-model:count="count"></HelloWorld></div>
</template><style lang="scss" scoped></style>

:

<script setup lang="ts">
defineProps<{count: number
}>()
defineEmits<{(e: 'update:count', newCount: number): void
}>()
</script><template><div>{{ count }}<button @click="$emit('update:count', count + 1)">+1</button></div>
</template><style lang="scss" scoped></style>

3、总结

Vue2中得 v-model 默认解析成 :value 与 @input

Vue3中得 v-model 默认解析成 :modelValue 与 @update:modelValue


Vue2中得 :attr.sync 默认解析成 :attr 与 @update:attr

Vue3中得 v-model:attr 默认解析成 :attr 与 @update:attr


作用:用于在自定义组件中实现父子组件之间的双向数据绑定

也就是说在Vue3中只有v-model,没有.sync

如果父组件只写v-model="xxx",子组件中接收到的props为modelValue ,抛出的自定义事件为update:modelValue

如果父组件写v-model:attr="xxx" (attr为自定义变量),子组件中接收到的props为attr ,抛出的自定义事件为update:attr

即:Vue3中使用双向数据绑定v-model时props不局限于只能接收modelValue,可以自定义

使用方式:

父: 
<HelloWorld v-model="count"></HelloWorld>子:
defineProps<{modelValue: number
}>()
defineEmits<{(e: 'update:modelValue', newCount: number): void
}>()
$emit('update:modelValue', modelValue + 1)"=====================================================================================父:
<HelloWorld v-model:count="count"></HelloWorld>子:
defineProps<{count: number
}>()
defineEmits<{(e: 'update:count', newCount: number): void
}>()
$emit('update:count', count + 1)">

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/119857.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OpenAI大模型项目计划表(InsCode AI 创作助手)

OpenAI大模型项目计划表 阶段任务负责人开始日期完成日期立项确定项目目标和范围项目经理2023-05-012023-05-03确定项目团队和资源项目经理2023-05-042023-05-05确定项目时间表和里程碑项目经理2023-05-062023-05-10数据收集收集训练数据和标注数据团队2023-05-112023-05-20确…

Elasticsearch部署中的两大常见问题及其解决方案

随着大数据和实时搜索的日益普及&#xff0c;Elasticsearch已经成为现代应用中不可或缺的工具。但是&#xff0c;像所有软件一样&#xff0c;部署和配置Elasticsearch可能会遇到一些问题。本文将探讨两个我最近遇到的常见问题及其解决方案。 问题描述 1. 主机名解析问题 在启…

设计模式——访问者模式(Visitor Pattern)+ Spring相关源码

文章目录 一、访问者模式&#xff08;Visitor Pattern&#xff09;二、文字描述三、例子例子一&#xff1a;菜鸟教程对象定义访问者定义使用总结 例子二&#xff1a;Spring的BeanDefinitionVisitor 一、访问者模式&#xff08;Visitor Pattern&#xff09; 行为型模式。 目的&…

测试C#调用Vlc.DotNet组件播放视频

除了Windows Media Player组件&#xff0c;在百度上搜索到还有不少文章介绍采用Vlc.DotNet组件播放视频&#xff0c;关于Vlc.DotNet的详细介绍见参考文献1&#xff0c;本文学习Vlc.DotNet的基本用法。   VS2022中新建基于.net core的winform程序&#xff0c;在Nuget包管理器中…

疫情物资管理系统-基于SSM实现

包括-源码参考文档 下载地址: https://juzhendongli.store/commodity/details/5

PDF编辑工具Acrobat Pro DC 2023中文

Acrobat Pro DC 2023是一款全面、高效的PDF编辑和管理软件。它提供了丰富的PDF编辑功能&#xff0c;如创建、编辑、合并、分割、压缩、旋转、裁剪等&#xff0c;让用户可以轻松处理各种PDF文档。同时&#xff0c;该软件还具有智能的PDF处理技术&#xff0c;可以自动识别和修复P…

iPhone手机屏幕分辨率

ios app测试时&#xff0c;需要测试应用在不同型号的苹果手机上的表现形式&#xff0c;可以自己在浏览器上配置。 代数设备逻辑像素尺寸缩放发布时间第一代iPhone 2G320 x 480480 x 3203.5寸1x2007年6月29日第二代iPhone 3320 x 480480 x 3203.5寸1x2008年7月11日第三代iPhone …

Iterator 和 ListIterator 的区别(简要说明)

Iterator 和 ListIterator 的区别 ListIterator有add()方法&#xff0c;可以向List中添加对象&#xff0c;而Iterator不能 ListIterator和Iterator都有hasNext()和next()方法&#xff0c;可以实现顺序向后遍历&#xff0c;但是ListIterator有hasPrevious()和previous()方法&am…

微前端qiankun接入Vue和React项目

主应用&#xff1a;Vue3Webpack 1、创建主应用&#xff1a; npx vue create main-vue3-app 2、安装qiankun npx yarn add qiankun 3、项目中使用的vue、vue-router、qiankun依赖如下&#xff0c;webpack版本为5.x 4、在根目录下创建vue.config.js const { defineConfig }…

【第25例】IPD体系进阶:需求分析团队RAT

目录 简介 RAT CSDN学院相关内容推荐 作者简介 简介 RAT是英文Requirement Analysis Team英文首字母的简称,也即需求分析团队,每个产品线都需要设定对应的一个RAT的组织。 RAT主要负责产品领域内需求的分析活动,是RMT的支撑团队: 这个时候可以将RAT细化为PL-RAT团队,…

常用应用安装教程---在centos7系统上安装Docker

在centos7系统上安装Docker 1&#xff1a;切换镜像源 wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo2&#xff1a;查看当前镜像源中支持的docker版本 yum list docker-ce --showduplicates | sort -r3&#x…

Spring Kafka生产者实现

需求 我们需要通过Spring Kafka库&#xff0c;将消息推送给Kafka的topic中。这里假设Kafka的集群和用户我们都有了。这里Kafka认证采取SASL_PLAINTEXT方式接入&#xff0c;SASL 采用 SCRAM-SHA-256 方式加解密。 pom.xml <dependency><groupId>org.springframew…

云原生架构设计理论与实践

云原生架构设计理论与实践 云原生架构概述 云原生的背景 云原生定义和特征 云原生架构的设计原则 架构模式 服务化架构模式 Mesh化架构模式 Serverless模式 存储计算分离模式 分布式事务模式 可观测架构 事件驱动架构 云原生架构相关技术 容器技术 云原生微服务技术 无服务…

MessageQueue 深入理解Android卷2 学习笔记

MessageQueue类封装了与消息队列有关的操作&#xff0c;一个以消息驱动的系统中&#xff0c;最重要部分就是消息队列和消息处理循环。 MessageQueue的核心代码在native层&#xff0c;可以处理java和native的事件 1.1MessageQueue创建 构造方法&#xff0c;调用nativeInit fra…

java实现hbase数据导出

1. HBase-client方式实现 1.1 依赖 <!--HBase依赖坐标--><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.2.6</version></dependency><dependency><group…

CVE-2021-44228 Apache log4j 远程命令执行漏洞

一、漏洞原理 log4j(log for java)是由Java编写的可靠、灵活的日志框架&#xff0c;是Apache旗下的一个开源项目&#xff0c;使用Log4j&#xff0c;我们更加方便的记录了日志信息&#xff0c;它不但能控制日志输出的目的地&#xff0c;也能控制日志输出的内容格式&#xff1b;…

CSDN学院 < 华为战略方法论进阶课 > 正式上线!

目录 你将收获 适用人群 课程内容 内容目录 CSDN学院 作者简介 你将收获 提升职场技能提升战略规划的能力实现多元化发展综合能力进阶 适用人群 主要适合公司中高层、创业者、产品经理、咨询顾问&#xff0c;以及致力于改变现状的学员。 课程内容 本期课程主要介绍华为…

不同网段的IP怎么互通

最近在整理工作的时候发现一个不同网段无法互通的问题&#xff0c;就是我们大家熟知的一级路由和二级路由无法互通的问题。由于需要记录整个过程的完整性&#xff0c;这里也需要详细记录下整个过程&#xff0c;明白的人不用看&#xff0c;可以直接跳过&#xff0c;到解决方法去…

GSCoolink GSV6182 带嵌入式MCU的MIPI D-PHY转HDMI 2.0

Gscoolink GSV6182是一款高性能、低功耗的MIPI D-PHY到HDMI 2.0转换器。通过集成基于RISC-V的增强型微控制器&#xff0c;GSV6182创造了一种具有成本效益的解决方案&#xff0c;提供了上市时间优势。MIPI D-PHY接收器支持CSI-2版本1.3和DSI版本1.3&#xff0c;每条通道最高可达…

网络建设 之 React数据管理

React作为一个用于构建用户界面的JavaScript库&#xff0c;很多人认为React仅仅只是一个UI 库&#xff0c;而不是一个前端框架&#xff0c;因为它在数据管理上是缺失的。在做一个小项目的时候&#xff0c;维护的数据量不多&#xff0c;管理/维护数据用useState/useRef就足够了&…