| // CustomInput.vue |
| <script setup lang="ts"> |
| import { defineModel } from 'vue' |
| const inputVal = defineModel() |
| </script> |
| |
| <template> |
| <div class="custom-input"> |
| <input v-model="inputVal" type="text" /> |
| </div> |
| </template> |
| |
| // 父组件 |
| <script lang="ts" setup> |
| import { ref } from 'vue' |
| import CustomInput from './CustomInput.vue' |
| |
| const inputValue = ref('hello defineModel') |
| </script> |
| |
| <template> |
| <div> |
| <CustomInput v-model="inputValue" /> |
| <p>Input value: {{ inputValue }}</p> |
| </div> |
| </template> |