vue3中的v-model语法糖

Vue2的v-model默认解析成 :value 与 @input
Vue3的 V-model 默认解析成 :modelValue 与 @update:modelValue
vue3中只需要 v-model 指令可以支持对个数据在父子组件同步,不再支持 .sync 语法
vue3 中 v-model 语法糖
:modelValue="count"@update:modelValue="count=$event"
vue3 中 v-model:xxx 语法糖?
:xxx="count"@update:xxx="count=$event"

方式一 通过 v-model 解析成 modelValue @update:modelValue

子组件

<script setup lang="ts">
defineProps<{
     
  modelValue: number
}>()

defineEmits<{
      (e: 'update:modelValue', count: 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">
const count = ref(10)
</script>
<template>
 <!--组件 -->
    <!--<cp-radio-btn
     :model-value="count"
     @updata:model-value="count = $event"
   ></cp-radio-btn>-->
    <!--  :model-value="count"@updata:model-value="count = $event"
    以这样的形式写,就可以简写为 v-model="count"依旧生效 -->
  <cp-radio-btn v-model="count"></cp-radio-btn>
</template>

<style lang="scss" scoped></style>

点击按钮,计数器+1
在这里插入图片描述

方式二: 通过 v-model:count 解析成 count @update:count

子组件

<script setup lang="ts">
defineProps<{
     
  count: number
}>()

defineEmits<{
      (e: 'update:count', count: number): void }>()
</script>

<template>
  <div>
    计数器{
  { count }}
    <button @click="$emit('update:count', count + 1)">+1</button>
  </div>
</template>

<style lang="scss" scoped></style>

父组件

<script setup lang="ts">
const count = ref(10)
</script>
<template>
 <!--组件 -->
  <cp-radio-btn v-model:count="count"></cp-radio-btn>
</template>

<style lang="scss" scoped></style>

相关推荐

  1. vuesetup语法优点

    2023-12-10 18:24:02       43 阅读
  2. vue2和vue3v-model

    2023-12-10 18:24:02       44 阅读
  3. Vue技巧】vue3不支持.sync语法解决方案

    2023-12-10 18:24:02       68 阅读
  4. vue3 setup语法

    2023-12-10 18:24:02       59 阅读
  5. 解决vue3不支持.sync语法

    2023-12-10 18:24:02       56 阅读
  6. Vue3setup()语法语法用法

    2023-12-10 18:24:02       49 阅读

最近更新

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

    2023-12-10 18:24:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 18:24:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 18:24:02       82 阅读
  4. Python语言-面向对象

    2023-12-10 18:24:02       91 阅读

热门阅读

  1. ESP32网络编程-OTA方式升级固件(基于Web浏览器)

    2023-12-10 18:24:02       60 阅读
  2. 如何选择Docker基础镜像

    2023-12-10 18:24:02       49 阅读
  3. 代码随想录 70. 爬楼梯

    2023-12-10 18:24:02       53 阅读
  4. 【前端设计模式】之责任链模式

    2023-12-10 18:24:02       75 阅读
  5. SAP-PP:超实用的表PP顾问必备

    2023-12-10 18:24:02       48 阅读
  6. 深入了解linux下网卡防火墙selinux

    2023-12-10 18:24:02       64 阅读
  7. 力扣:200. 岛屿数量(Python3)

    2023-12-10 18:24:02       64 阅读