Vue3 如何实现一个全局搜索框

2023-10-31

效果:点击搜索或者按下ctrl+k键 ,页面就会出现搜索框

搜索框页面(SearchBar.vue)

搜索框的ts   (SearchBar.ts)

封装的搜索框hook (useSearch.ts)

在app.vue中出现搜索框 (App.vue)

1.搜索框页面(SearchBar.vue)(搜索框样式自己写)

<template>
<div id="searchBarWrapper"  ref="searchBarWrapper" class="search"
>
     <div class="searchBox">
          <div class="topChange">
            <div class="botBox">
              <el-input
                v-model="inputLeft"
                class="w-50 m-2"
                :placeholder="placeholderText" >
                
                  <!-- @blur="blurChange" -->
                <template #suffix>
                  <el-icon class="el-input__icon" 
                    ><search
                  /></el-icon>
                </template>
              </el-input>
            </div>
          </div>
        </div>
</div>
</template>

<script lang="ts" setup>
import { defineComponent,nextTick } from 'vue'
nextTick(()=>{
    //自动对焦
    cleanSerachKeyword();
    inmpuModal.value?.focus();
})
</script>

<style>
.search{
    width: 300px;
    height: 40px;
    background-color: aqua;
}
</style>

2.搜索框的ts   (SearchBar.ts)

import { h, render } from "vue"
import SearchBar from "./SearchBar.vue"
export default class SearchBarCreator {
  container: HTMLElement
  appElement: HTMLElement | null
  showing: boolean
  _dismiss: () => void
  constructor() {
    this.container = document.createElement("div")
    this.showing = false
    this.appElement = document.body.querySelector("#app")
    this.present.bind(this)
    this.dismiss.bind(this)
    this._dismiss = this.dismiss.bind(this)
  }

  present() {
    // console.log('进入方法了')
    // const SearchBar2 = h(SearchBar);//虚拟dom生成VNode
    // console.log('1')
    // render(SearchBar2, this.container)//VNode生成真是DOM
    // console.log('2')
    // document.body.insertBefore(this.container, document.body.firstChild)


    if (this.showing) {
      this.dismiss()
    } else {
      const SearchBar2 = h(SearchBar)
      render(SearchBar2, this.container)
      const searchBarWrapperDOM =
        this.container.querySelector("#searchBarWrapper")
      searchBarWrapperDOM?.classList.add("animate-searchInputAnimation")
      document.body.insertBefore(this.container, document.body.firstChild)
      this.showing = true
      this.appElement?.addEventListener("click", this._dismiss)
    }
  }

  dismiss() {
    // if(this.container){
    //     render(null,this.container);
    //     document.body.removeChild(this.container);
    // }

    
    if (this.showing && this.container) {
      render(null, this.container)
      document.body.removeChild(this.container)
      this.showing = false
      this.appElement?.removeEventListener("click", this._dismiss)
    } else {
      console.log("不需要关闭")
    }
  }
}

3.封装的搜索框hook (useSearch.ts)

import SearchBarCreator from "../views/exe/searchBar/searchBar"
  const searchBar=new SearchBarCreator();
function openSearchBar(){
  searchBar.present();
}
function closeSearchBar(){
  searchBar.dismiss();
}
export default{
    openSearchBar,
    closeSearchBar
};

 4.App.vue

<script  lang="ts" setup>
import {
  ref,
  defineComponent,
  getCurrentInstance,
  nextTick,
  onMounted,
  onUpdated,
} from "vue";
import { RouterView } from "vue-router";
import { useRouter } from "vue-router";
import GlobalHeader from "./components/GlobalHeader/index.vue";
import GlobalFooter from "./components/GlobalFooter/index.vue";
import backTop from "./components/backTop/index.vue"
import useSearch from "./hook/useSearch"
const {openSearchBar,closeSearchBar}=useSearch
onMounted(()=>{
  window.addEventListener("keydown",(e)=>{
    if(e.ctrlKey && e.key==="k"){
      //同时按下 ctrl 和 k
      openSearchBar()
    }
  })
})
</script>

<template>
  <RouterView />
  <!-- <div>
    <button @click="openSearchBar">搜索</button>
      <button @click="closeSearchBar">关闭</button>
  </div> -->

</template>
<style scoped>
@keyframes searchInput {
  from{
    transform:translateY(50px);
  }
  to{
    transform: translateY((0px));
  }
}
.searchInput{
  animation: searchInput 1s;
}
 html,body,#app{
  min-height: 100vh;
}
</style>
  

 

 聚焦输入框:

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue3 如何实现一个全局搜索框 的相关文章

随机推荐

  • Maven篇:搭建私有仓库之Nexus

    环境准备 linux版本 CentOS Linux release 7 9 1804 JDK版本 jdk 8u191 linux i586 Maven版本 apache maven 3 6 3 Nexus版本 nexus 3 53 0 01
  • java四种权限修饰符

    访问权限修饰符 public 意为公开的 访问权限最高 可以跨包访问 protect 意为受保护的 权限次之 可以在同包和子 父类中访问 default 意为默认的 一般不写 权限次之 可以在同包中访问 private 意为私有的 权限最低
  • 自动化测试优势&劣势

    目录 一 自动化测试概述 二 自动化测试优势 劣势 优势 劣势 三 自动化测试常用工具 代码级别 接口 协议级别 界面级别 一 自动化测试概述 软件自动化测试是相对手工测试而存在的 由测试人员根据测试用例中描述的规程一步步执行测试 得到实际
  • 创建chrome右键菜单划词搜索扩展

    转载请注明出处 http blog csdn net zhymax article details 8552830 上网时经常在多个搜索引擎间切换 但使用chrome自带的搜索引擎切换比较麻烦 换一个引擎就需要设置一次配置 因此也在chro
  • Working routine【Codeforces 706 E】【二维链表】

    Codeforces Round 367 Div 2 E 可以说是一道模拟题了 写了有些时候 可能是太菜了吧 题意 给出一个原始矩阵 之后有Q次操作 我们将两个矩阵交换位置 题目中保证两个矩阵不相交 给出的是两个矩阵的左上方的端点 以及它们
  • 机器学习sklearn-特征工程

    数据挖掘的5大流程 1 获取数据 2 数据预处理 3 特征工程 将原始数据转换为更能代表预测模型的潜在问题的特征的过程 可以通过挑选最相关的特征 提取特征以及创建特征来是想 4建模 测试模型并预测结果 5 上线模型 特征工程 sklearn
  • Qt也有垃圾回收(通过QScopedPointer实现),下决心在项目里使用QScopedPointer,省了太多事情了,而且更安全!!...

    也谈Qt的垃圾回收 前几天在做代码审核的时候 Kai Uwe Broulik建议使用QScopedPointer来替代手工内存管理 使用后发觉确实节约了不少代码量 我的CHERRY可以延长寿命了 但是通过简单地阅读代码 发现和Python等
  • Ubuntu18.04 ROS Melodic的cv_bridge指向问题(四种方式,包括opencv4)

    备注 2023 7 4修改 如果是ros空间 可以在工作空间中使用单独cv bridge的方式 比较简单 是我目前常用的方式 放在文章最后 由于ROS Melodic自带的是Opencv3 2 0 而我自己下载的是opencv3 4 5 所
  • AndroidFFmpeg

    https github com appunite AndroidFFmpeg git 本地路径 这个比较全 四种播放方式都有 问题流媒体播放无控制 太快太慢都报错 lbg android ffmpeg AndroidFFmpeg http
  • 机器学习葡萄酒质量_通过数据和机器学习制作出更好的啤酒和葡萄酒

    机器学习葡萄酒质量 带GPS的狗 电子鼻和可倾倒完美啤酒的机器人 GPS Wearing Dogs an Electronic Nose and a Robot That Pours the Perfect Beer Bushfires i
  • Hal库自动生成Freertos时出现osSemaphoreCreate和osSemaphoreWait报错

    由于freertos和Hal版本问题 生成的函数会生成老版本的 所以不兼容 需要改掉 将osSemaphoreCreate osSemaphore SEM 1 改成 osSemaphoreNew 1 1 osSemaphore SEM in
  • TimeGAN学习记录

    一 学习TimeGAN主要参考的链接如下 1 知乎上的TimeGAN论文研读 2 csdn上的一篇博客 论文阅读 Time Series Generative Adversrial Networks TimeGAN 时间序列GAN 3 时间
  • 使用神经网络对黄金期货交割价格进行预测-4 MATLAB

    上一篇文章讲述了如何对预测的结果进行合理化修正 本文主要讲述的是对神经网络本身的学习算法进行优化 一般优化神经网络有三种模式 一种为优化神经网络的连接结构 一种为优化神经网络的学习算法 一种为既优化连接结构 又优化学习算法 由于笔者的知识水
  • JetBrains下载历史版本

    https www jetbrains com clion download other html 在上方的链接中将clion改为idea phpstrom webstrom等等 转载于 https www cnblogs com yang
  • WuThreat身份安全云-TVD每日漏洞情报-2023-10-08

    漏洞名称 Glibc ld so本地权限提升漏洞 漏洞级别 高危 漏洞编号 CVE 2023 4911 相关涉及 系统 ubuntu 22 04 glibc Up to excluding 2 35 0ubuntu3 4 漏洞状态 POC
  • java调用.so文件

    第一步 public class JavaToCTest private native void sayHello 声明本地方法 static System loadLibrary JavaToCTest 需要加载的so库文件的名称 在li
  • iptables的CONNMARK与MARK

    iptables的CONNMARK与MARK Posted on January 24 2012 iptables的CONNMARK与MARK是用于给数据连接和数据包打标记的两个target 一直没搞明白二者的区别 直到昨天花了不少时间解决
  • 【zookeeper】fsync-ing the write ahead log in took which will adversely effect operation latency

    1 概述 原文 zookeeper fsync ing the write ahead log in took which will adversely effect operation latency 在解决上一个问题的时候遇到这个问题的
  • windows应用商店或者其他微软应用打不开的修复办法

    最近突然发现微软自带的那个便笺打不开了 想在应用商店里面重装一下 又发现windows store也变成灰色的 并且有个小叹号 打不开了 右键这个store 设置里面重置也没用 最后在某乎上面看了个回答 照着试了试 发现可以了 这里把步骤记
  • Vue3 如何实现一个全局搜索框

    效果 点击搜索或者按下ctrl k键 页面就会出现搜索框 搜索框页面 SearchBar vue 搜索框的ts SearchBar ts 封装的搜索框hook useSearch ts 在app vue中出现搜索框 App vue 1 搜索