当前位置: 首页 > news >正文

vue2迁移到vue3,v-model的调整

项目从vue2迁移到vue3,v-model不能再使用了,需要如何调整?

下面只提示变化最小的迁移,不赘述vue2和vue3中的常规写法。

vue2迁移到vue3,往往不想去调整之前的代码,以下就使用改动较小的方案进行调整。

In terms of what has changed, at a high level:

  • BREAKING: When used on custom components, v-model prop and event default names are changed:
    • prop: value -> modelValue;
    • event: input -> update:modelValue;
  • BREAKING: v-bind's .sync modifier and component model option are removed and replaced with an argument on v-model;
  • NEW: Multiple v-model bindings on the same component are possible now;
  • NEW: Added the ability to create custom v-model modifiers.

For more information, read on!

以上文档来自v-model | Vue 3 Migration GuideGuide on migrating from Vue 2 to Vue 3icon-default.png?t=N7T8https://v3-migration.vuejs.org/breaking-changes/v-model.html

翻译结果:

就发生的变化而言,在高水平上:
BREAKING:在自定义组件上使用时,v模型道具和事件默认名称会发生更改:
prop:value->modelValue;
event:input->update:modelValue;
BREAKING:v-bind的.sync修饰符和组件模型选项被删除,并替换为v-model上的一个参数;
新功能:现在可以在同一组件上进行多个v模型绑定;
新增:增加了创建自定义v型模型修改器的功能。
有关更多信息,请继续阅读!

下面看代码调整:

简单写一个例子(vue2): 

<template><view @click="onAdd">{{ value }}</view>
</template>
<script>
export default {props: {value: Number},methods: {onAdd(){this.$emit('input', this.value + 1)}}
}
</script>

上面改成(vue3)

<template><view @click="onAdd">{{ modelValue }}</view>
</template>
<script>
export default {props: {modelValue: Number},methods: {onAdd(){this.$emit('update:modelValue', this.value + 1)}}
}
</script>

 下面将支出vue2迁至vue3需要修改的地方,见下图:

 

http://www.lryc.cn/news/341357.html

相关文章:

  • 【C语言】解决不同场景字符串问题:巧妙运用字符串函数
  • android 如何显示网络地址对应的图片
  • 【项目学习01_2024.05.02_Day04】
  • Android SQLite中的UNIQUE constraint failed错误
  • 嘴尚绝卤味:传承经典,缔造美食新风尚
  • Docker 入门与实践:从零开始构建容器化应用环境
  • C# 用户控件UserControl事件解绑资源释放
  • QT-QTCreator环境配置
  • 最大连续和
  • 分布式系统事务一致性解决方案(基于事务消息)
  • Unity Animation--动画剪辑
  • 如何将 redis 快速部署为 docker 容器?
  • iOS - Undefined symbols: 解决方法
  • 优化理论复习——(三)
  • RK3568笔记二十四:基于Flask的网页监控系统
  • [Django 0-1] Core.Serializers 模块
  • 鸿蒙内核源码分析(用栈方式篇) | 程序运行场地谁提供的
  • Linux 进程间通信之匿名管道
  • 数据结构与算法学习笔记六--数组和广义表(C语言)
  • 图搜索算法详解
  • 安卓中常见的UI控件
  • 基于Labelme的背部穴位关键点制作
  • go-mysql-transfer 同步数据到es
  • 外包干了3天,技术就明显退步了。。。。。
  • 将要上市的自动驾驶新书《自动驾驶系统开发》中摘录各章片段 1
  • String、StringBuilder、StringBuffer之间的区别是什么?
  • docker系列8:容器卷挂载(上)
  • 痉挛性斜颈患者自己做哪些运动对脖子好?
  • 数据结构——二叉树链式结构的实现(上)
  • 数据结构内容概览