11.智能搜索案例:封装搜索框

Search.vue组件 主要做的事情是:

1.用户输入内容的时候,出现右侧“x”关闭图标

2. 右侧“x”关闭图标,的作用:就是点击它,可以清空文本框中的内容

3.文本框内容,输入的内容,设置上限,最多允许输入内容 不超过20个字。

4.文本框的外形---是方形,还是圆形?

通过 给自己 添加 自定义属性props~~~ shape

5.父组件app.vue,传入数据

传入 round,显示是圆
<search shape="round"></search>
传入square,显示是方形
<search shape="square"></search>

html:

<template>
    <div class="search-body">
        <!-- 搜索输入框 -->
        <div class="search-input-body">
            <input type="text" v-model="kw" class="search-input-el" :style="{borderRadius:rarius}">
            <span class="clear-btn" @click="clearKw" v-show="kw.length>0">x</span>
        </div>
    </div>
</template>

css:

<style scoped>
.search-body{
    display: flex;
}
/* 包裹input和x按钮的div盒子 */
.search-input-body{
    width: 200px;
    height: 30px;
    line-height: 30px;
    position: relative;
    margin-right: 10px;
}
.search-input-el{
    width: 200px;
    height: 30px;
    border: 0;
    outline: none;
    background: #eee;
}
.clear-btn{
    position: absolute;
    top: 0;
    right: 10px;
    cursor: pointer;
}
</style>

js:

<script>
export default {
    // 自定义2个属性,用于父组件在使用这个组件的时候,向这个组件里传值
    // 这2个属性的数据类型设置如下
    props:{
        shape:{
            type:String,
            default:'square',
            validator:function(val){
                return ['square','round'].includes(val)
            }
        },
        clearable:{
            type:Boolean,
            default:true
        }
    },
    data() {
        return {
            // 搜索关键字
            kw:''
        }
    },
    watch:{
        // 数据监听kw里的值,不允许用户输入超过20个字符
        kw(val,oldVal){
            if(val.length>20){
                this.kw = oldVal
            }
        }
    },
    computed:{
        // rarius依赖于响应式数据(shape)的,rarius这个值,是通过一系列运算得到。
        rarius(){
            let val;
            if(this.shape == 'round'){
                val = '20px';
            }else{
                val = '0px';
            }
            return val
        }
    },
    methods: {
        clearKw(){
            this.kw = ''
        }
    },
};
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值