# 自定义组件上使用v-model监听

{% hint style="info" %}
父组件
{% endhint %}

```
<template>
    <search v-model="keywords" placeholder="请输入查询内容"></search>
</template>

<script>
    import search from './search.vue'
    export default {
        components: {
            search
        },
        data() {
            return {
                keywords: ''
            }
        },
        watch: {
            keywords(val) {
                console.log(val, 'v-model监听的值')
            }
        }
    }
</script>
```

{% hint style="info" %}
子组件
{% endhint %}

```
// search.vue

<template>
    <div class="search-box">
        <div class="search-content">
            <span class="icon-search"></span>
            <input type="text" v-model="currentValue" :placeholder="placeholder" :autofocus="autofocus" @click="isShow = true" @blur="outBlur">
            <a href="javascript:;" @click="isShow = false, currentValue = ''" v-show="isShow">取消</a>
        </div>
    </div>
</template>

<script>
export default {
    props: {
        value: {
            type: String,
            default: ''
        },
        placeholder: {
            type: String,
            default: '请输入'
        },
        autofocus: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return{
            currentValue: this.value,
            isShow: this.value ? true : false
        }
    },
    watch: {
        currentValue(val) {
            this.$emit('input', val)
            this.isShow = val
        },
        value(val) {
            this.currentValue = val
        }
    },
    methods: {
        outBlur() {
            if (!this.currentValue) {
                this.isShow = false
            }
        }
    }
}
</script>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yicoding.gitbook.io/vue/feng-zhuang-zu-jian/zi-ding-yi-zu-jian-shang-shi-yong-vmodel-jian-ting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
