vue3 封装自定义组件v-model的示例

作者:当代词圣李白 时间:2024-06-05 10:05:23 

首先要注意 vue3中 v-model 默认绑定的变量名变了,从原理的 value 改成了 modelValue,
如果要改变变量的值,要执行一个事件 this.$emit("update:modelValue", value);

vue3 封装自定义组件v-model的示例

vue3 封装自定义组件v-model的示例

<template>
<div class="inline">
<input :type="password ? 'password' : 'text'" ref="input" @input="handleInput" @blur="handleBlur($event.target.value)" :placeholder="placeholder" />
</div>
</template>
<script>
export default {
name: "dg-Input",
props: {
type: {
type: String,
requided: true,
},
placeholder: {
type: String,
},
password: {
default: false,
},
modelValue: [String, Number],
},
data() {
return {};
},
computed: {
nativeInputValue() {
return this.modelValue === null || this.modelValue === undefined ? "" : String(this.modelValue);
},
},
methods: {
handleInput(event) {
let value = event.target.value;
this.$emit("update:modelValue", value);
this.$emit("input", value);
this.$nextTick(this.setNativeInputValue);
},
setNativeInputValue() {
const input = this.$refs.input;
if (input.value === this.nativeInputValue) return;
input.value = this.nativeInputValue;
},

},
mounted() {
this.setNativeInputValue();
},
};
</script>
<style lang="scss" scoped>
.inline {
display: inline-block;
input {
width: 100%;
height: 100%;
padding-left: 5px;
}
}
</style>

vue3文档地址

补充:下面看下vue3中自定义组件使用v-model

vue2 中的v-model

v-model本质上是一个语法糖,如下代码

<input v-model="test">
<!--上面代码本质上就是下方代码-->
<input :value="test" @input="test = $event.target.value">

因此,对于一个带有 v-model 的组件(核心用法),它应该如下:

带有v-model的父组件通过绑定的value值(即v-model的绑定值)传给子组件,子组件通过 prop接收一个 value;
子组件利用oninput事件实时通过 $emit 触发父组件input 事件,并传入新值value给父组件;

父组件

<template>
   <div>
       <child v-model="msg" @input="msg = $event.target.value" />
       <!--<child :value="msg" @input="msg = $event.target.value" />-->
   </div>
</template>
<script>
import child from './components/Child.vue'
export default {
   components: {
       child
   },
   data() {
       return {
           msg: ''
       }
   }
}
</script>

子组件child

<template>
   <input type="text" :value="modelValue" @input="handleInput">
</template>
<script>
export default {
   name: 'Child',
   props:['value'],
   data() {
       return {
           modelValue: this.value
       }
   },
   methods: {
       handleInput(event) {
           this.$emit('input', event)
       }
   }
}

vue3中的 v-model

vue3中的v-model默认绑定的不是value,而是modelValue,接收的方法由input改为@update:modelValue。

<template>
 <child v-model="msg" />
 <p>{{msg}}</p>
</template>

<script lang="ts">
import { defineComponent,ref} from 'vue';
import child from './components/child/index.vue'

export default defineComponent({
 name: 'App',
 components:{
   child
 },
 setup(){
   const msg = ref('1')
   return{
     msg
   }
 }
});
</script>
<template>
   <input type="text" :value="modelValue" @input="onInput">
</template>
<script lang="ts">
import {defineComponent} from 'vue'
export default defineComponent({
   name:'ChildInput',
   props:{
       modelValue:{
           type:String
       }
   },
   setup(props, context){
       const onInput = (e: Event) =>{
           context.emit('update:modelValue',e.target.value)
       }
       return{
           onInput
       }
   }
})
</script>

来源:https://blog.csdn.net/qq_32486011/article/details/112613434

标签:vue,自定义组件,v-model
0
投稿

猜你喜欢

  • 总结一些js自定义的函数

    2024-04-19 10:43:54
  • Python和C/C++交互的几种方法总结

    2021-08-25 00:49:06
  • Python实现淘宝秒杀功能的示例代码

    2021-05-26 09:41:49
  • MSSQL 游标使用 心得

    2024-01-21 02:00:48
  • Python np.where()的详解以及代码应用

    2023-02-15 18:33:01
  • Python中Yield的基本用法

    2021-08-30 15:34:55
  • Pygame Transform图像变形的实现示例

    2022-03-04 03:39:29
  • 链接与文本标签们

    2008-04-04 18:07:00
  • Python实现将内容转为base64编码与解码

    2021-11-25 14:44:24
  • 使用sklearn进行对数据标准化、归一化以及将数据还原的方法

    2022-03-28 19:44:27
  • 通过Python爬虫代理IP快速增加博客阅读量

    2023-08-17 16:17:55
  • php生成curl命令行的方法

    2023-07-23 22:19:42
  • chr()函数参照表 chr13 chr10 chr34

    2009-09-03 13:22:00
  • QT连接Oracle数据库并实现登录验证的操作步骤

    2024-01-27 13:06:44
  • Django模板之基本的 for 循环 和 List内容的显示方式

    2021-09-24 05:18:24
  • 详解Django框架中的视图级缓存

    2021-02-02 02:13:33
  • 8个js表单验证函数

    2007-10-28 19:19:00
  • python生成圆形图片的方法

    2021-04-09 14:48:15
  • 如何基于pythonnet调用halcon脚本

    2022-09-27 16:34:23
  • Django使用Celery实现异步发送邮件

    2022-11-04 17:50:47
  • asp之家 网络编程 m.aspxhome.com