vue封装好看的搜索框组件,input自动获取焦点

我们做vue项目,一般会封装一些常用的组件,今天我就把我自己封装的搜索框分享给大家,样式简洁漂亮,点击搜索,input输入框会自动获取焦点。如下图所示
在这里插入图片描述
在这里插入图片描述

html部分

<template>
  <div class="search_box">
    <div class="search_btn" @click="onShow" v-if="!isShow">
      <img @click="clickSearch" src="@/assets/images/icon_search.png" />
      <span>搜索</span>
    </div>
    <div class="search_input" v-if="isShow">
      <img @click="clickSearch" src="@/assets/images/icon_search.png" />
      <input
        v-if="isShow"
        v-model="searchKey"
        placeholder="搜索"
        @blur="clickSearch"
        @keyup.13="clickSearch"
        ref="input"
      />
    </div>
    <a v-if="isShow" @click="onShow">取消</a>
  </div>
</template>

js部分

<script>
import { navPage } from "../api/api";
export default {
  data() {
    return {
      searchKey: "",
      isShow: false,
    };
  },
  methods: {
    onShow() {
      this.isShow = !this.isShow;
      if (this.isShow) {
        //input 自动获取焦点
        this.$nextTick(() => {
          this.$refs.input.focus()
        });
      }
      this.searchKey = "";
      this.clickSearch();
    },
    clickSearch() {
      let param = {
        moduleCode: "xmzl_zxjd",
        keyword: this.searchKey.trim(),
      }; 
      navPage(param).then((res) => {
        if (res.data && res.data.success) {
          let searchData = res.data.responData.part3[0].items;
          // 将多个参数传给父组件
          this.$emit("onSearch", searchData, this.searchKey);
        }  
      });
    },
  },
};
</script>

css部分

<style lang="less" scoped>
.search_box {
  margin-bottom: 20px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  > a {
    color: #2d6efb;
    font-size: 14px;
    margin-left: 12px;
  }
  .search_input {
    flex: 1;
    height: 36px;
    background: #f7f7f7;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 16px;
      height: 16px;
      margin: 0 12px;
    }
    input {
      flex: 1;
      height: 100%;
      font-size: 14px;
      outline: none;
      background: transparent;
      color: #666;
    }
    input:-moz-placeholder {
      /* Mozilla Firefox 4 to 18 */
      color: #999999;
    }
    input::-moz-placeholder {
      /* Mozilla Firefox 19+ */
      color: #999999;
    }
    input:-ms-input-placeholder {
      color: #999999;
    }
    input::-webkit-input-placeholder {
      color: #999999;
    }
  }
  .search_btn {
    flex: 1;
    height: 36px;
    background: #f7f7f7;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 16px;
      height: 16px;
      margin: 0 12px;
    }
    span {
      font-size: 16px;
      color: #999999;
    }
  }
}
</style>

父组件调用

<search @onSearch="onSearch"></search>

//获取子组件传过来的值
onSearch(searchData, searchKey) {  
  this.searchKey = searchKey;
  this.searchData = searchData; 
},
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值