Vue 自定义搜索输入框SearchInput

发布时间:2023年12月17日

效果如下:

组件代码

<template>
    <div class="search-input flex flex-space-between flex-center-cz">
        <input type="text" v-model="value" :ref="inpuName" :placeholder="placeholder" @keyup.enter="searchClick"/>
        <img class="pointer" src="@/assets/images/find-blue.png" @click="searchClick">
    </div>
</template>

<script>
    export default {
        name:'SearchInput',
        props:{
            placeholder:{},
            name:{},
            focus:{}
        },
        created(){
            this.inpuName = this.name
        },
        mounted() {
            if (this.focus == 'focus') {
                this.$nextTick(() => {
                    this.$refs[this.inpuName].focus();
                })
            }

        },
        data() {
            return {
                value:''
            }
        },
        methods:{
            searchClick() {
                this.$emit("search", this.value);
            },

        }
    }
</script>

<style scoped>

  .search-input {
    box-sizing: border-box;
    border-radius: 5px;
    padding-left: 8px;
    padding-right: 8px;
    height: 28px;
    background: rgba(255,255,255,0.2);
    box-shadow: 0px 4px 9px 0px rgba(0,81,139,0.16);
    border-radius: 12px;
  }
  .search-input input {
    border: 0;
    width: 100%;
    background: transparent;
  }
  .search-input img {
    width: 12px;
    height: 12px;
  }

</style>

调用代码

 <SearchInput  :placeholder="'请输入文档名或内容查询'" style="width: 300px;" @search="search"></SearchInput>



文章来源:https://blog.csdn.net/gdgztt/article/details/135049255
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。