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需要修改的地方,见下图:

 

相关推荐

  1. vue2vue3v-model

    2024-05-01 08:50:03       44 阅读
  2. Vue2v-model

    2024-05-01 08:50:03       43 阅读
  3. Vue技巧】Vue2Vue3组件上使用v-model实现原理

    2024-05-01 08:50:03       58 阅读
  4. 详解vue3组件 v-model

    2024-05-01 08:50:03       39 阅读
  5. Vue3 v-model使用

    2024-05-01 08:50:03       36 阅读
  6. Vue2Vue3 v-model 区别#记录

    2024-05-01 08:50:03       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-01 08:50:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-01 08:50:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-01 08:50:03       82 阅读
  4. Python语言-面向对象

    2024-05-01 08:50:03       91 阅读

热门阅读

  1. pnpm:基础使用及详解

    2024-05-01 08:50:03       31 阅读
  2. C语言-预处理

    2024-05-01 08:50:03       26 阅读
  3. Hardened Ubuntu 24.04 LTS发布

    2024-05-01 08:50:03       26 阅读
  4. Web前端面试题(持续更新中)

    2024-05-01 08:50:03       32 阅读
  5. VSCode便捷版的制作

    2024-05-01 08:50:03       28 阅读
  6. 利用R语言自带函数快速探索数据

    2024-05-01 08:50:03       32 阅读
  7. Python内置函数iter()详解

    2024-05-01 08:50:03       28 阅读
  8. Seata分布式原理及优势

    2024-05-01 08:50:03       43 阅读
  9. 鼠标界面的隐藏显示

    2024-05-01 08:50:03       25 阅读
  10. 安卓手机APP开发__媒体开发部分__媒体投屏

    2024-05-01 08:50:03       33 阅读