vue3 封装自定义组件v-model的示例
作者:当代词圣李白 发布时间:2024-06-05 10:05:23
标签:vue,自定义组件,v-model
首先要注意 vue3中 v-model 默认绑定的变量名变了,从原理的 value 改成了 modelValue,
如果要改变变量的值,要执行一个事件 this.$emit("update:modelValue", value);
<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


猜你喜欢
- Python语言中import的使用很简单,直接使用 import module_name 语句导入即可。这里我主要写一下"imp
- 需求每天往一个表里面插入两条数据,但日期不同INSERT INTO test(`id`, `art_training_institution
- 总的来说:1、数据库设计和表创建时就要考虑性能2、sql的编写需要注意优化3、分区、分表、分库设计表的时候:1、字段避免null值出现,nu
- 最近想尝试一下在IBM Bluemix上使用Python语言创建Web应用程序,所以需要在本地搭建Python Web的开发测试环境。关于P
- 重读LukeW的《Web Form Design:Filling in the Blanks》感触很深,除佩服LukeW的钻研精神外,更多的
- python提供了4种方式来满足进程间的数据通信1. 使用multiprocessing.Queue可以在进程间通信,但不能在Pool池创建
- 本文实例讲述了python二分法查找算法实现方法。分享给大家供大家参考,具体如下:二分法查找二分查找又称折半查找,优点是比较次数少,查找速度
- 1.global关键字默认情况下,在局部作用域对全局变量只能进行:读取,修改内部元素(可变类型),无法对全局变量进行重新赋值读取:CITY=
- 一、创建堆heapq有两种方式创建堆, 一种是使用一个空列表,然后使用heapq.heappush()函数把值加入堆中,另外一种就是使用he
- 在网上搜索的时候,经常看到两种打开方式: dispatch和EnsureDispatchimport win32com.client as
- vue项目中在可编辑div光标位置插入内容html:<div class="mouse-move fl f12 h22 lh
- 问题描述python的pandas库中有一个十分便利的isnull()函数,它可以用来判断缺失值,我们通过几个例子学习它的使用方法。首先我们
- 啥都不说了,直接奉献原代码 代码如下:'==========注意==================================
- 前言本文使用 cpu 版本的 TensorFlow 2.4 ,分别搭建单层 Bi-LSTM 模型和多层 Bi-LSTM 模型完成文本分类任务
- 定义切片区别于数组,是引用类型, 不是值类型。数组是固定长度的,而切片长度是可变的,我的理解是:切片是对数组一个片段的引用。var s1 [
- 本文实例为大家分享了pygame实现非图片按钮效果的具体代码,供大家参考,具体内容如下按钮类程序# -*- coding=utf-8 -*-
- 在使用Django过程中需要开发一些API给其他系统使用,为了安全把Token等验证信息放在header头中。如何获取:使用request.
- 本文所述实例可以实现基于Python的查看图片报纸《参考消息》并将当天的图片报纸自动下载到本地供查看的功能,具体实现代码如下:# codin
- 变量类型ECMAScript变量可能包含两种不同类型的数据值:基本类型和引用类型。基本类型基本类型指的是简单的数据段,5种基本数据类型:un
- 刚才运行了一段代码,来查看Request.ServerVariables里面有多少值,看了一下,共50个!代码<%=Request.S