vue3 的v-model语法糖

这篇具有很好参考价值的文章主要介绍了vue3 的v-model语法糖。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Vue2的v-model默认解析成:value与@input
Vue3的v-model默认解析成:modelValue与@update:modelValue

所以要是想通过封装组件支持v-model的使用,用v-model语法糖无疑是最好的选择

子组件定义使用modelValue进行接收父组件传递过来的值,定义事件update:modelValue通知父组件改变一些事情

举例使用:封装子组件用法:cp-radio-btn

//子组件 cp-radio-btn

<script setup lang="ts">
// 通过v-model双向绑定实现计数器
const props = defineProps<{
  modelValue: number
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', count: number): void
}>()
const btnClick = () => {
  emit('update:modelValue', props.modelValue + 10)
}
</script>

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

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

父组件patient -使用子组件cp-radio-btn

使用 modelValue 像子组件传递数据,定义子组件通知的事件 @update:modelValue做事情

//父组件 patient

<script setup lang="ts">
import { ref, onMounted } from 'vue'

const count = ref(0)

const updateCount = (num: number) => {
  count.value = num
}
</script>

<cp-radio-btn :modelValue="count" @update:modelValue="updateCount"></cp-radio-btn>
//<cp-radio-btn :modelValue="count" @update:modelValue="count = $event"></cp-radio-btn>

如此就完成了数据的双向绑定;

在这,:modelValue="count" @update:modelValue="updateCount"就可以简写为v-model="count"

可以实现同上代码同样的效果 

<cp-radio-btn v-model="count"></cp-radio-btn>

上面文章开始说了Vue3的v-model默认是解析成了:modelValue与@update:modelValue,下面说一下,如果你想修改这个默认的传值与事件的话,如何修改?

举个例子:这里换传值事件名字时(这里用OtherName举例)

使用v-model后面需要加上:OtherName

<cp-radio-btn v-model:OtherName="count"></cp-radio-btn>
子组件的传值以及事件都需要改为自定义的名称

<script setup lang="ts">
// 通过v-model双向绑定实现计数器
const props = defineProps<{
  OtherName: number
}>()

const emit = defineEmits<{
  (e: 'update:OtherName', OtherName: number): void
}>()
const btnClick = () => {
  emit('update:OtherName', props.OtherName + 10)
}
</script>

<template>
  <div class="cp-radio-btn">
    计数器:{{ OtherName }} <br />
    <button @click="btnClick">修改OtherName</button>
  </div>
</template>

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

 封装一个自定义单选按钮组件 cp-radio-btn,满足下方需求

 vue3 的v-model语法糖

贴出代码 :

<script setup lang="ts">
defineProps<{
  options: { label: string; value: number | string }[]
  modelValue?: number | string
}>()
defineEmits<{
  (e: 'update:modelValue', value: number | string): void
}>()
</script>

<template>
  <div class="cp-radio-btn">
    <a
      class="item"
      href="javascript:;"
      v-for="item in options"
      :key="item.value"
      :class="{ active: modelValue == item.value }"
      @click="$emit('update:modelValue', item.value)"
      >{{ item.label }}</a
    >
  </div>
</template>

<style lang="scss" scoped>
.cp-radio-btn {
  display: flex;
  flex-wrap: wrap;
  .item {
    height: 32px;
    min-width: 60px;
    line-height: 30px;
    padding: 0 14px;
    text-align: center;
    border: 1px solid var(--cp-bg);
    background-color: var(--cp-bg);
    margin-right: 10px;
    box-sizing: border-box;
    color: var(--cp-text2);
    margin-bottom: 10px;
    border-radius: 4px;
    transition: all 0.3s;
    &.active {
      border-color: var(--cp-primary);
      background-color: var(--cp-plain);
    }
  }
}
</style>

 使用组件:

<cp-radio-btn :options="timeOptions" v-model="form.illnessTime" />

使用vant 的van-action-sheet动作面板组件封装一个支付组件 cp-pay-sheet

vue3 的v-model语法糖

cp-pay-sheet

<script setup lang="ts">
import { showToast, showLoadingToast } from 'vant'
import { ref } from 'vue'
import { getConsultOrderPayUrl } from '@/services/consult'

const props = defineProps<{
  orderId: string
  actualPayment: number
  onClose?: () => void
  show: boolean
}>()
const emit = defineEmits<{
  (e: 'update:show', val: boolean): void
}>()

const paymentMethod = ref<0 | 1>()

// 跳转支付
const pay = async () => {
  if (paymentMethod.value === undefined) return showToast('请选择支付方式')
  showLoadingToast({ message: '跳转支付', duration: 0 })
  const res = await getConsultOrderPayUrl({
    orderId: props.orderId,
    paymentMethod: paymentMethod.value,
    payCallback: 'http://localhost:5173/room'
  })
  window.location.href = res.data.payUrl
}
</script>

<template>
  <!-- 支付方式弹窗 -->
  <van-action-sheet
    :show="show"
    @update:show="emit('update:show', $event)"
    title="选择支付方式"
    :close-on-popstate="false"
    :closeable="false"
    :before-close="onClose"
  >
    <div class="pay-type">
      <p class="amount">¥20.00</p>
      <van-cell-group>
        <van-cell title="微信支付" @click="paymentMethod = 0">
          <template #icon><cp-icon name="consult-wechat" /></template>
          <template #extra><van-checkbox :checked="paymentMethod == 0" /></template>
        </van-cell>
        <van-cell title="支付宝支付" @click="paymentMethod = 1">
          <template #icon><cp-icon name="consult-alipay" /></template>
          <template #extra><van-checkbox :checked="paymentMethod == 1" /></template>
        </van-cell>
      </van-cell-group>
      <div class="btn">
        <van-button type="primary" round block @click="pay">立即支付</van-button>
      </div>
    </div>
  </van-action-sheet>
</template>

<style lang="scss" scoped>
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .icon-page {
      margin-right: 10px;
      font-size: 18px;
      margin-top: 6px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>

 使用组件文章来源地址https://www.toymoban.com/news/detail-437882.html

    <cp-pay-sheet

      v-model:show="show"

      :order-id="orderId"

      :actual-payment="payInfo.actualPayment"

      :on-close="onClose"

    ></cp-pay-sheet>

到了这里,关于vue3 的v-model语法糖的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue3 系列:自定义 v-model

    1. input 中的 v-model 2. naive-ui 组件二次封装 v-model

    2024年02月07日
    浏览(34)
  • vue3 子组件实现v-model用法

    在Vue 3中,实现自定义的 input 组件并支持 v-model 绑定,涉及到对 modelValue 这个默认prop的处理和对应的 update:modelValue 事件的触发。Vue 3使得这个过程比Vue 2更为简化和灵活,尤其是在可以自定义绑定的属性和事件名方面。 步骤 1: 创建自定义Input组件 首先,创建一个自定义的I

    2024年04月27日
    浏览(38)
  • 组件v-model(.sync)记录使用(vue3)

    首先,让我们来了解一下Vue3中v-model的用法。在Vue3中, v-model 指令可以用于自定义组件上,用于实现组件的双向数据绑定。与Vue2中的 .sync 不同, Vue3中的v-model需要在组件中手动实现双向绑定逻辑。 下面是一个简单的父组件示例,展示了如何在Vue3中使用 v-model 来实现组件的双

    2024年01月19日
    浏览(40)
  • 前端Vue篇之v-model 是如何实现的,语法糖实际是什么?v-model 可以被用在自定义组件上吗?如果可以,如何使用?

    v-model 在 Vue.js 中扮演着重要的角色,实现了表单输入和应用状态之间的双向数据绑定。其具体实现方式取决于所绑定元素的类型。 作用在表单元素上 : 当 v-model 用于表单元素(如 input、textarea)时,它动态绑定了 input 的 value 到指定的变量,并在触发 input 事件时动态更新这

    2024年04月28日
    浏览(39)
  • vue3探索——组件通信之v-model父子组件数据同步

    再很多场景中,我们可能想在子组件中修改父组件的数据,但事实上,vue不推荐我们这么做,因为数据的修改不容易溯源。 在vue2中,我们使用 .sync 修饰符+自定义事件 \\\'update:xxx\\\' ,来使父子组件数据同步。 这里不作过多说明,有需要请自行了解。 vue3的写法与vue2基本一致。最

    2024年02月11日
    浏览(55)
  • Vue3-在HTML标签、组件标签上使用v-model

    本篇为Vue3学习中遇到的v-model相关的记录,如有问题欢迎指正 v-model通常在input、select等标签上来实现数据双向绑定 原理 :v-model通过给标签value赋值来实现  数据—页面 的绑定。然后通过绑定input事件实现 页面—数据 的绑定。 原理 :在组件上时,v-model通过 :modelValue 来进行

    2024年01月24日
    浏览(56)
  • Vue3.2+TS的组件间的v-model传值

    组件之间的v-model,为什么可以v-model,大家可以去看看v-model的原理,然后就会发现这个方法一目了然。 父组件 子组件 假如子组件需要按照某个方法变化,那么可以自己定义,比如需要自增加50

    2024年02月13日
    浏览(41)
  • vue3中l和vue2中v-model不同点

    vue2比较让人诟病的一点就是提供了两种双向绑定:v-model和.sync, 在vue3中,去掉了.sync修饰符,只需要使用v-model进行双向绑定即可。 为了让v-model更好的针对多个属性进行双向绑定(vue2中自定义组件中v-model只能使用一次),vue3做出了以下修改: 1、当对自定义组件使用v-mod

    2024年01月21日
    浏览(34)
  • 【Vue技巧】Vue2和Vue3组件上使用v-model的实现原理

    ChatGPT4.0国内站点,支持GPT4 Vision 视觉模型:海鲸AI 在Vue中, v-model 是一个语法糖,用于在输入框、选择框等表单元素上创建双向数据绑定。当你在自定义组件中实现 v-model 功能时,你需要理解它背后的原理: v-model 实际上是一个属性和一个事件的简写。 在 Vue 2.x 中, v-mode

    2024年01月15日
    浏览(57)
  • vue项目中对组件使用v-model绑定值,在vue3中如何更新数据

    在el-form 中 el-form-item 绑定组件进行校验 想在表单下面爆红提示 可以对组件使用v-model绑定值 vue2 通过this.$emit(‘input’,value) 更新 v-model值 vue3 通过this.$emit(‘update:modelValue’ ,value) 更新 v-model值

    2024年02月15日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包