<script setup>
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
</script>
<template>
<input
:value="props.modelValue"
@input="emit('update:modelValue', $event.target.value)"
/>
</template>
<Child
:modelValue="foo"
@update:modelValue="$event => (foo = $event)"
/>
<UserName
v-model:first-name="first"
v-model:last-name="last"
/>
vue
<script setup>
const firstName = defineModel('firstName')
const lastName = defineModel('lastName')
</script>
<template>
<input type="text" v-model="firstName" />
<input type="text" v-model="lastName" />
</template>
<script setup>
defineProps({
firstName: String,
lastName: String
})
defineEmits(['update:firstName', 'update:lastName'])
</script>
<template>
<input
type="text"
:value="firstName"
@input="$emit('update:firstName', $event.target.value)"
/>
<input
type="text"
:value="lastName"
@input="$emit('update:lastName', $event.target.value)"
/>
</template>
<template>
<div class="child-con">
<p>我是子组件2</p>
<button @click="toParentEmits">点击传到父组件</button><br>
子组件输入框name<input v-model="name" placeholder="输入姓名" @input="changeName(name)" />
子组件输入框age<input v-model="age" placeholder="输入姓名" @input="changeAge(age)" />
</div>
</template>
<script setup lang="ts">
import {ref,defineEmits,computed } from "vue";
const childValue = ref("我是子组件的值childValue")
const emits = defineEmits(['update:name','update:age','getChild']);
const props = defineProps({
name: { type: String, default: "张三" },
age: { type: Number, default: 18 }
})
const name = computed({
get: () => props.name,
set: (val) => {
emits('update:name', val)
}
})
const age = computed({
get: () => props.age,
set: (val) => {
emits('update:age', val)
}
})
const changeName = (val) => {
emits('update:name', val)
}
const changeAge = (val) => {
emits('update:age', val)
}
const toParentEmits = () =>{
emits('getChild',childValue)
}
</script>