使用了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

最新文章

  1. ASCII码表
  2. Linux与user和group相关文件分析
  3. Jenkins的maven工程打包的时候怎么指定不同环境的配置文件
  4. Lambert漫反射.BLinnPhong及Phong模型 Unity自带的在Lighting.cginc里
  5. 使用DotNetOpenAuth搭建OAuth2.0授权框架——Demo代码简单说明
  6. IBM X3650 M4服务器安装centos找不到硬盘的解决方法
  7. 数据库订正脚本性能优化两则:去除不必要的查询和批量插入SQL
  8. Spring MVC学习笔记 01
  9. mysql数据库文件默认保存目录(windows)
  10. 机器更换登录密码重启,然后SQL Server登录不了
  11. Perl数组: shift, unshift, push, pop
  12. Niagara解决设备连接应用的软件框架平台技术。
  13. [Python]再学 socket 之非阻塞 Server
  14. C# ftp 图片上传多快好省
  15. C#实现的apache htpasswd加密
  16. editplus 常用正则
  17. Mysql参数汇总
  18. 《DOM Scripting》学习笔记-——第八章 充实文档的内容
  19. rt-thread是如何做到通过menuconfig配置将相应文件加入工程和从工程中除去
  20. 伪分布式hadoop1.1.2和hbase0.94.11配置

热门文章

  1. vue.js----之router详解(三)
  2. 全面jmeter逻辑控制器案例详解
  3. Linux内核编译中的各类错误示例
  4. 在nestjs中使用rabbitmq
  5. 搭建 springboot 应用
  6. C#消息泵探索(一)
  7. GUI程序设计--班级信息收集系
  8. debian11命令行安装字体
  9. Spring Boot应用启动
  10. JMeter创建上传文件脚本