当前位置: 首页 > article >正文

vue2 系列:自定义 v-model

1. input 中的 v-model

<!-- 表单双向绑定 -->
<input :value="username" @input="username = $event.target.value" />
<!-- 等于 -->
<input v-model="username" />

2. 自定义组件 v-model

<!-- 组件双向绑定 -->
<!-- 子 -->
<script>
export default {
  props: {
    value: {
      required: true,
    },
  },
  watch: {
    value(newValue) {
      this.my_input = newValue;
    },
  },
  data() {
    return {
      my_input: this.value,
    };
  },
  methods: {
    handleChange() {
      this.$emit("input", this.my_input);
    },
  },
};
</script>
<template>
  <el-input v-model="my_input" @change="handleChange"></el-input>
</template>

<!-- 父 -->
<my-component v-model="username" />


http://www.kler.cn/a/105238.html

相关文章:

  • 【机器学习合集】激活函数合集 ->(个人学习记录笔记)
  • 文件上传漏洞(1), 文件上传绕过原理
  • php-手动搭建windows的php和nginx、phpmyadmin环境
  • uniapp把文件中的内复制到另一个文件中
  • 数据清洗与规范化详解
  • 分布式事务——CAP理论 解决分布式事务的思路 Seata组件初识 和 部署
  • linux实现基础网络库(socket,epoll,pthread,cmake,pipe, sem,codition,)
  • 一台服务器最大能支持多少条 TCP 连接
  • nginx负载均衡配置
  • 逐字稿 | 视频理解论文串讲(下)【论文精读】
  • 【数据结构】ST 表与 RMQ 算法
  • 如何把项目上传到Gitee(详细教程)
  • elasticsearch-5.6.15集群部署,如何部署x-pack并添加安全认证
  • 理解V3中的proxy和reflect
  • 【0229】libpq库实现压测PG服务器max_connections的最大连接数
  • CSS动画 transition和animation
  • MongoDB 学习笔记(基础)
  • Ceres 使用笔记
  • 【QT】信号和槽
  • layui移除(删除)table表格的一行