vue3封装input组件
使用了2种方法去封装input组件(.vue与.jsx)
代码如下
父组件:
<template>
<div>
<h1>input组件封装</h1>
<Input v-model:val="val"></Input>
<InputJsx v-model:valJsx="valJsx"></InputJsx>
<button @click="getVal">获取input</button>
</div>
</template> <script lang="ts">
import { reactive, toRefs, ref, defineComponent } from "vue";
import HelloWorld from "@/components/HelloWorld.jsx";
import Input from "@/components/Input.vue";
import InputJsx from "@/components/Input.jsx";
export default defineComponent({
name: "",
components: {
HelloWorld,
Input,
InputJsx,
},
setup() {
const data = reactive({});
const val = ref("测试");
const valJsx = ref("测试jsx");
const getVal = () => {
console.log("val值" + val.value);
console.log("valJsx值" + valJsx.value);
};
return {
val,
valJsx,
getVal,
...toRefs(data),
};
},
});
</script>
<style scoped lang="less"></style>
子组件:jsx写法
import { defineComponent, ref, watch } from "vue";
import "@/style/test.less";
export default defineComponent({
props: {
valJsx: {
type: String,
default: "",
},
},
setup(props, { emit }) {
const changeVal = (event) => {
console.log(event.target.value);
emit("update:valJsx", event.target.value);
};
return (props) => {
return (
<div>
<input value={props.valJsx} onChange={changeVal} type="text" />
</div>
);
};
},
});
子组件:vue写法
<template>
<div>
<input :value="val" @input="$emit('update:val', $event.target.value)" type="text" />
</div>
</template> <script lang="ts">
import { toRefs, defineComponent, watch } from "vue";
export default defineComponent({
name: "Input",
props: {
val: {
type: String,
default: "",
},
},
emits: ["update:val"],
setup(props, { emit }) {
watch(
() => props.val,
(newVal) => {
emit("update:val", newVal);
},
);
return {
...toRefs(props),
};
},
});
</script>
<style scoped lang="less"></style>
多个v-model绑定
子组件:
<template>
<div>title: </div>
<input
type="text"
:value="title"
@input="$emit('update:title', $event.target.value)">
<span>{{title}}</span>
<div></div>
<div>description: </div>
<input
type="text"
:value="description"
@input="$emit('update:description', $event.target.value)">
<span>{{description}}</span>
</template> <script>
export default {
props: {
title: String,
description: String
},
emits: ['update:title', 'update:description'],
}
</script>
父组件:
<template>
<CustomInput v-model:title="title" v-model:description="description"></CustomInput>
</template> <script>
import CustomInput from '../components/CustomInput.vue' export default {
components: { CustomInput },
data() {
return {
title: '',
description: '',
}
}
}
</script>
完结:2021-09-06 17:27:02
vue3封装input组件的更多相关文章
- Vue3 封装第三方组件(一)做一个合格的传声筒
各种UI库的功能都是非常强大的,尤其对于我这种不会 css 的人来说,就更是帮了大忙了. 只是嘛,如果再封装一下的话,那么用起来就会更方便了. 那么如何封装呢? 封装三要素 -- 属性.插槽.事件.方 ...
- 从0搭建vue3组件库: Input组件
本篇文章将为我们的组件库添加一个新成员:Input组件.其中Input组件要实现的功能有: 基础用法 禁用状态 尺寸大小 输入长度 可清空 密码框 带Icon的输入框 文本域 自适应文本高度的文本域 ...
- 纯生js实现Element中input组件的部分功能(慢慢完善)并封装成组件
现在实现的有基础用法.可清空.密码框,参考链接:https://element.eleme.cn/#/zh-CN/component/input HTML代码:想要测试哪个组件,直接将对应组件解开注释 ...
- 08 - Vue3 UI Framework - Input 组件
接下来再做一个常用的组件 - input 组件 返回阅读列表点击 这里 需求分析 开始之前我们先做一个简单的需求分析 input 组件有两种类型,即 input 和 textarea 类型 当类型为 ...
- Vue3 封装 Element Plus Menu 无限级菜单组件
本文分别使用 SFC(模板方式)和 tsx 方式对 Element Plus el-menu 组件进行二次封装,实现配置化的菜单,有了配置化的菜单,后续便可以根据路由动态渲染菜单. 1 数据结构定义 ...
- 使用shadow dom封装web组件
什么是shadow dom? 首先我们先来看看它长什么样子.在HTML5中,我们只用写如下简单的两行代码,就可以通过 <video> 标签来创建一个浏览器自带的视频播放器控件. <v ...
- asp.net core封装layui组件示例分享
用什么封装?自然是TagHelper啊,是啥?自己瞅文档去 在学习使用TagHelper的时候,最希望的就是能有个Demo能够让自己作为参考 怎么去封装一个组件? 不同的情况怎么去实现? 有没有更好更 ...
- 封装input 逐渐,且input插件必须带有默认值。
封装input 逐渐,且input插件必须带有默认值. 组件: <template> <div class="input-show"> <span c ...
- vue 封装分页组件
分页 一般都是调接口, 接口为这种格式 {code: 0, msg: "success",…} code:0 data:{ content:[{content: "11& ...
- 自己封装js组件 - 中级中高级
接着做关于alert组件的笔记 怎么又出来个中高级呢 对没错 就是出一个中高级来刷流量呵呵呵,但是中高级也不是白叫的 这次主要是增加了widget类,增加了自己绑定的事件和触发事件的方法!这么做是为什 ...
随机推荐
- oracle 根据逗号拆分字符串一行转多行
SELECT A.*, REGEXP_SUBSTR( A.PRODUCTNUMS, '[^,]+', 1, L ) AS PRODUCTNUM,L FROM LG_ZJQH_PRODUCTVALUES ...
- python + QML程序中调用WebView后打包
QML中如果加入了WebView控件,在用pyinstaller打包时不会自动导入,从而导致打包出的程序运行报错,no WebView plugin found,此时需要手动将WebView控件复制到 ...
- C++中map用法详解(转)
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数: map<stri ...
- Windows 与Docker
https://docs.microsoft.com/zh-cn/windows/wsl/install-manual#step-4---download-the-linux-kernel-updat ...
- 快速使用时序数据库InfluxDB
快速使用时序数据库InfluxDB InfluxDB是一款优秀的时间序列数据库,适合存储设备性能.日志.物联网传感器等带时间戳的数据. 1. InfluxDB主要特性 InfluxDB也就是TSDB, ...
- springboot gradle 加速问题
初始化项目使用阿里云 seriver url : https://start.spring.io 直接修改为: https://start.aliyun.com 关键的gradle 修改安装包地址 g ...
- GO 语言中的 sync Map
为什么需要 sync map go 语言之所以引入 sync.Map主要是因为GO 语言自带的 map 是线程不安全的.只能保证并发的读,但是不能保证并发的写. 看下面的例子: func main() ...
- TIDB-DM数据迁移第一部(安装部署)
官方连接: https://docs.pingcap.com/zh/tidb/stable/dm-overview 架构: 1.安装DM download https://tiup-mirrors.p ...
- 网线接口调试,Android ADB网络调试!
ADB网络调试,网线接口调试 没有USB接口,照样可以调试,可通过网线接口调试步骤! 一.第一步连接WIFI 查看wifi 的IP win+R 键 打开运行 ,输入cmd 二.输入ipc ...
- jdbc封装工具类(无连接池)
package com.huang; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepar ...