vue2 系列:自定义 v-model

1. input 中的 v-model

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

2. 自定义组件 v-model

html 复制代码
<!-- 组件双向绑定 -->
<!-- 子 -->
<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" />
相关推荐
kyriewen1134 分钟前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
Timer@2 小时前
LangChain 教程 04|Agent 详解:让 AI 学会“自己干活“
javascript·人工智能·langchain
阿珊和她的猫2 小时前
TypeScript中的never类型: 深入理解never类型的使用场景和特点
javascript·typescript·状态模式
skywalk81632 小时前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
RopenYuan4 小时前
FastAPI -API Router的应用
前端·网络·python
走粥5 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory0015 小时前
layui select重新渲染
前端·layui
weixin199701080166 小时前
《中国供应商商品详情页前端性能优化实战》
前端·性能优化
九皇叔叔6 小时前
003-SpringSecurity-Demo 统一响应类
java·javascript·spring·springsecurity