4.组件间通信-v-model

vue3组件间通信-v-model

父组件:

javascript 复制代码
<template>
  <div class="father">
    <h3>父组件</h3>
    <!-- <h4>{{ username }}</h4>
    <h4>{{ password }}</h4> -->
    <!-- v-model用在html标签上 -->
    <!-- <input type="text" v-model="username"> <br>
    <input type="text" :value="username" @input="username=(<HTMLInputElement>$event.target).value"> -->
    <!-- v-model用在组件标签上 -->
    <AtguiguInput v-model="username"/>
    <!-- 对于原生事件,$event就是事件对象,能$event.target.value
         对于自定义事件,$event就是触发事件时所传递的数据,不能$event.target.value,只能$event
    --> 
    <!-- <AtguiguInput 
      :modelValue="username" 
      @update:modelValue="username = $event"
    /> -->

    <!-- 修改modelValue -->
    <!-- <AtguiguInput v-model:ming="username" v-model:mima="password"/> -->
  </div>
</template>
<script setup lang="ts" name="Father">
	import { ref } from "vue";
  import AtguiguInput from './AtguiguInput.vue'
  // // 数据
  let username = ref('zhansgan')
  let password = ref('123456')
</script>
<style scoped>
.father {
  padding: 20px;
  background-color: rgb(165, 164, 164);
  border-radius: 10px;
}
</style>

子组件:

javascript 复制代码
<template>
  <input 
    type="text" 
    :value="modelValue"
    @input="emit('update:modelValue',(<HTMLInputElement>$event.target).value)"
  >
  <br>
   <!-- 对于原生事件,$event就是事件对象,能$event.target.value
         对于自定义事件,$event就是触发事件时所传递的数据,不能$event.target.value,只能$event
    --> 
  <input 
    type="text" 
    :value="mima"
    @input="emit('update:mima',(<HTMLInputElement>$event.target).value)"
  >
</template>
<script setup lang="ts" name="AtguiguInput">
  defineProps(['modelValue','mima'])
  const emit = defineEmits(['update:modelValue','update:mima'])
</script>

<style scoped>
  input {
    border: 2px solid black;
    background-image: linear-gradient(45deg,red,yellow,green);
    height: 30px;
    font-size: 20px;
    color: white;
  }
</style>
相关推荐
布列瑟农的星空10 小时前
Playwright使用体验
前端·单元测试
卤代烃11 小时前
🦾 可为与不可为:CDP 视角下的 Browser 控制边界
前端·人工智能·浏览器
_XU11 小时前
AI工具如何重塑我的开发日常
前端·人工智能·深度学习
C_心欲无痕11 小时前
vue3 - defineExpose暴露给父组件属性和方法
前端·javascript·vue.js·vue3
鹿人戛11 小时前
HarmonyOS应用开发:相机预览花屏问题解决案例
android·前端·harmonyos
萌萌哒草头将军11 小时前
绿联云 NAS 安装 AudioDock 详细教程
前端·docker·容器
计算机毕设VX:Fegn089512 小时前
计算机毕业设计|基于springboot + vue宠物医院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
贺今宵12 小时前
安装better-sqlite3报错electron-vite
javascript·sql·sqlite·sqlite3
GIS之路12 小时前
GIS 数据转换:使用 GDAL 将 GeoJSON 转换为 Shp 数据
前端
2501_9444460012 小时前
Flutter&OpenHarmony文件夹管理功能实现
android·javascript·flutter