无能吐槽一下,虽然用了很多遍v-model,但是还是不得要领,每次看官网都感觉说的不是很清晰,在写的时候还是要查看文档,可能就是不理解原理,这次特意好好写一篇文章,让自己好好理解一下。
假设我们自定义一个搜索框组件,样式肯定是比input 标签本身的好看,在配上搜索按钮
这里的代码就不描述了
搜索框输入的文本你怎么告诉别人,双向绑定呗
<template>
<div class="search_btn">
<div class="search_icon" />
<input />
</div>
</template>
<script setup lang="ts">
defineProps({
searchWord: {
type: String,
required: false
}
});
</script>
<template>
<div class="search_btn">
<div class="search_icon" />
<input :value="searchWord" />
</div>
</template>
<script setup lang="ts">
defineProps({
searchWord: {
type: String,
required: false
}
});
</script>
const emits = defineEmits(['update:searchWord']);
@input="$emit('update:searchWord', $event.target.value)"
<template>
<div class="search_btn">
<div class="search_icon" />
<input :value="searchWord" @input="$emit('update:searchWord', $event.target.value)"/>
</div>
</template>
<script setup lang="ts">
defineProps({
searchWord: {
type: String,
required: false
}
});
const emits = defineEmits(['update:searchWord']);
</script>
<main-search
placeholder="请输入企业名称"
v-model:search-word="keyWord"
/>