Vue3 中的 v-model 语法糖(三种写法)

Vue2 中的 v-model 默认解析为 :value 和 @input

Vue3 中的 v-model 默认解析为 :modelValue 和 @update:modelValue

Vue3 中的 v-model:attr 默认解析为 :attr和 @update:attr

Vue3 第一种写法  :modelValue 和 @update:modelValue

父组件:

<script setup lang="ts">
import { ref } from 'vue'
const count = ref(10)
</script>

<template>
  <son
    :model-value="count"
    @update:modelValue="count = $event"
  ></son>
</template>

子组件:

<script setup lang="ts">
// 计数器
// 通过 v-model 解析成 modelValue @update:modelValue
defineProps<{
  modelValue: number
}>()

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

<template>
  <div class="cp-radio-btn">
    计数器:{{ modelValue }}
    <button @click="$emit('update:modelValue', modelValue + 1)">+1</button>
  </div>
</template>

Vue3 第二种写法  v-model

父组件:

<script setup lang="ts">
import { ref } from 'vue'
const count = ref(10)
</script>

<template>
  <son v-model="count"></son>
</template>

子组件:

<script setup lang="ts">
// 计数器
// 通过 v-model 解析成 modelValue @update:modelValue
defineProps<{
  modelValue: number
}>()

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

<template>
  <div class="cp-radio-btn">
    计数器:{{ modelValue }}
    <button @click="$emit('update:modelValue', modelValue + 1)">+1</button>
  </div>
</template>

Vue3 第三种写法   通过v-model:count 解析成 count @update:count

父组件:

<script setup lang="ts">
import { ref } from 'vue'
const count = ref(10)
</script>

<template>
  <son v-model:count="count"></son>
</template>

子组件:

<script setup lang="ts">
// 计数器
// 通过 v-model:count 解析成 count @update:count
defineProps<{
  count: number
}>()

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

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

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

  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3 ,你可以使用 `setup` 语法糖来实现子组件向父组件传递数据和更新父组件的 `v-model`。下面是一个示例: ```vue <template> <div> <!-- 子组件 --> <ChildComponent v-model="childValue" /> <p>子组件的值: {{ childValue }}</p> </div> </template> <script> import { ref } from 'vue'; import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, setup() { const childValue = ref(''); return { childValue }; } }; </script> ``` 在这个例子,`ChildComponent` 是子组件,它接收一个 `v-model` 绑定的值 `childValue`。在父组件,我们使用 `setup` 函数来定义一个名为 `childValue` 的响应式变量,并将其传递给子组件。子组件可以通过修改 `childValue` 来更新父组件的值。 在子组件,你可以通过 `props` 接收 `childValue` 并将其绑定到一个内部变量上。然后,你可以在子组件使用 `$emit` 来向父组件发送更新事件。 ```vue <template> <div> <input type="text" :value="value" @input="updateValue($event.target.value)" /> </div> </template> <script> export default { props: ['value'], methods: { updateValue(newValue) { this.$emit('update:value', newValue); } } }; </script> ``` 在子组件,我们将 `value` 属性绑定到输入框的值,并通过 `@input` 事件监听输入框的变化。当输入框的值发生变化时,我们调用 `updateValue` 方法,并使用 `$emit` 发送一个名为 `update:value` 的事件,将新的值传递给父组件。 这样,在父组件,我们就可以通过监听子组件发出的 `update:value` 事件来更新父组件的值,并且这个更新会通过 `v-model` 双向绑定到子组件上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值