Vue3 v-model 指令用法详解

绑定单个 v-model 用法

父组件: 

<script setup lang="ts">
  import { ref } from "vue";
  import HomeView from "./views/HomeView.vue";
  let str1=ref('你好')
</script>

<template>
  <div>父组件:{{ str1 }}</div>
  <HomeView v-model="str1"></HomeView>
</template>

子组件: 

<script setup lang="ts">
  defineProps<{
    modelValue:string
  }>()
  const emit=defineEmits(['update:modelValue'])
  const func=()=>{
    emit('update:modelValue','我改变了父组件穿过来的值')
  }
</script>

<template>
  <button @click="func">按钮</button>
  <div>子组件:{{ modelValue }}</div>
</template> 

绑定多个 v-model 用法

父组件:

<script setup lang="ts">
  import { ref } from "vue";
  import HomeView from "./views/HomeView.vue";
  let str1=ref('你好')
  let str2=ref('测试')
</script>

<template>
  <div>父组件-str1:{{ str1 }}</div>
  <div>父组件-str2:{{ str2 }}</div>
  <HomeView v-model="str1" v-model:inputVal="str2"></HomeView>
</template>

 子组件:

<script setup lang="ts">
  defineProps<{
    modelValue:string
    inputVal:string
  }>()
  const emit=defineEmits(['update:modelValue','update:inputVal'])
  const func=()=>{
    emit('update:modelValue','我改变了父组件穿过来的值')
  }
  const valText=ref<string>('sdfsdf')
  const inputFunc=(e:Event)=>{
    const elementInput=e.target as HTMLInputElement
    emit('update:inputVal',elementInput.value)
  }
</script>

<template>
  <button @click="func">按钮</button>
  <div>子组件:{{ modelValue }}</div>
  <input @input="inputFunc" type="text" v-model="valText" />
  <div>子组件:{{ inputVal }}</div>
</template>

绑定 v-model 并添加自定义修饰符用法

父组件: 

<script setup lang="ts">
  import { ref } from "vue";
  import HomeView from "./views/HomeView.vue";
  let str1=ref('你好')
  let str2=ref('测试')
  let str3=ref('早上好')
</script>

<template>
  <div>父组件-str1:{{ str1 }}</div>
  <div>父组件-str2:{{ str2 }}</div>
  <div>父组件-str3:{{ str3 }}</div>
  <HomeView v-model="str1" v-model:inputVal.myModifier="str2"></HomeView>
  <HomeView v-model="str1" v-model:inputVal="str3"></HomeView>
</template>

子组件: 

<script setup lang="ts">
  const props=defineProps<{
    modelValue:string
    inputVal:string
    inputValModifiers?:{ // 这里 ? 表示修饰符可选
      myModifier:boolean
    }
  }>()
  const emit=defineEmits(['update:modelValue','update:inputVal'])
  const func=()=>{
    emit('update:modelValue','我改变了父组件穿过来的值')
  }
  const valText=ref<string>('input初始值')
  const inputFunc=(e:Event)=>{
    const elementInput=e.target as HTMLInputElement
    emit('update:inputVal',props?.inputValModifiers?.myModifier?elementInput.value+'添加了修饰符':elementInput.value)
  }
</script>

<template>
  <button @click="func">按钮</button>
  <div>子组件:{{ modelValue }}</div>
  <input @input="inputFunc" type="text" v-model="valText" />
  <div>子组件:{{ inputVal }}</div>
</template> 

注:在 Vue3 中 v-model 是破坏性更新的,v-model 其实是一个语法糖,通过 props 和 emit 组合而成

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
v-modelVue.js中常用的指令之一,它可以在表单控件元素上创建双向数据绑定。当表单控件元素的值发生变化时,绑定的数据也会随之变化,反之亦然。v-model的实现原理是通过在表单控件元素上监听input或者change事件,然后将表单控件元素的值同步到Vue实例中的数据上。 在组件中使用v-model时,需要使用props属性将父组件中的数据传递给子组件,然后在子组件中使用$emit方法触发自定义事件,将子组件中的数据传递给父组件。具体实现可以参考以下代码: ```vue // 父组件 <template> <div> <custom-input v-model="message"></custom-input> <p>父组件中的message值为:{{ message }}</p> </div> </template> <script> import CustomInput from './CustomInput.vue' export default { components: { CustomInput }, data() { return { message: '' } } } </script> // 子组件 CustomInput.vue <template> <div> <input :value="value" @input="$emit('input', $event.target.value)"> </div> </template> <script> export default { props: { value: String } } </script> ``` 在上述代码中,父组件中使用了自定义组件CustomInput,并将父组件中的message数据通过v-model绑定到CustomInput组件中。在CustomInput组件中,使用props属性接收父组件传递的数据,并在input元素上使用:value绑定props中的value值,同时监听input事件,通过$emit方法触发自定义事件input,并将input元素的值作为参数传递给父组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值