vue2 增加左侧目录搜索功能

2024-06-19 14:12

本文主要是介绍vue2 增加左侧目录搜索功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1:效果图如下

不多说上代码

2:引入方式 (fuse更多使用功能可以百度搜索)

方式一:cdn方式引入 fuse.js 轻量级模糊搜索,在html页面里面引入,引入后记得重启,不然不生效
https://cdn.bootcdn.net/ajax/libs/fuse.js/7.0.0/fuse.basic.min.js

方式二:也可以不用cdn方式,npm install fuse.js,需要在子组件里面加上这个 import Fuse from 'fuse.js'

3:可以把这个封装成一个组件使用  

父:

<headerSearch id="header-search" class="right-menu-item" />

import headerSearch from "./headerSearch";

components:{headerSearch}

子:就是下面代码 (cdn方式

<template><div :class="{'show':show}" class="header-search"><svg-icon class-name="search-icon" icon-class="search" @click.stop="click" /><el-selectref="headerSearchSelect"v-model="search":remote-method="querySearch"filterabledefault-first-optionremoteplaceholder="请输入要搜索的左侧目录"class="header-search-select"@change="change"><el-option v-for="item,index in options" :key="index" :value="item.item.urlLabel" :label="item.item.name.join(' > ')" /></el-select></div></template><script>// fuse is a lightweight fuzzy-search module// make search results more in line with expectations//import Fuse from 'fuse.js'import path from 'path'export default {name: 'HeaderSearch',data() {return {search: '',options: [],searchPool: [],show: false,fuse: undefined}},computed: {routes() {return this.$store.getters.navList}},watch: {routes() {this.searchPool = this.generateRoutes(this.routes)},searchPool(list) {this.initFuse(list)},show(value) {if (value) {document.body.addEventListener('click', this.close)} else {document.body.removeEventListener('click', this.close)}}},mounted() {this.searchPool = this.generateRoutes(this.routes)},methods: {click() {this.show = !this.showif (this.show) {this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()}},close() {this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()this.options = []this.show = false},change(val) {this.$router.push(val)this.search = ''this.options = []this.$nextTick(() => {this.show = false})},initFuse(list) {this.fuse = new Fuse(list, {shouldSort: true,threshold: 0.4,location: 0,distance: 100,maxPatternLength: 32,minMatchCharLength: 1,keys: [{name: 'name',weight: 0.7}, {name: 'urlLabel',weight: 0.3}]})},// Filter out the routes that can be displayed in the sidebar// And generate the internationalized titlegenerateRoutes(routes, basePath = '/', prefixTitle = []) {let res = []for (const router of routes) {// skip hidden routerif (router.hidden) { continue }// console.log(router)const data = {urlLabel: path.resolve(basePath, router.urlLabel),name: [...prefixTitle]}if (router && router.name) {data.name = [...data.name, router.name]// if (router.redirect !== 'noRedirect') {// only push the routes with title// special case: need to exclude parent router without redirectres.push(data)// }}// recursive child routesif (router.children) {const tempRoutes = this.generateRoutes(router.children, data.urlLabel, data.name)if (tempRoutes.length >= 1) {res = [...res, ...tempRoutes]}}}return res},querySearch(query) {if (query !== '') {this.options = this.fuse.search(query)} else {this.options = []}// console.log(this.options)}}}</script><style lang="scss" scoped>.header-search {font-size: 0 !important;.search-icon {cursor: pointer;font-size: 18px;vertical-align: middle;}.header-search-select {font-size: 18px;transition: width 0.2s;width: 0;overflow: hidden;background: transparent;border-radius: 0;display: inline-block;vertical-align: middle;::v-deep .el-input__inner {border-radius: 0;border: 0;padding-left: 0;padding-right: 0;box-shadow: none !important;border-bottom: 1px solid #d9d9d9;vertical-align: middle;}}&.show {.header-search-select {width: 210px;margin-left: 10px;}}}</style>

5:数据结构

        routes() {

        return this.$store.getters.navList

      }

navList对应的数据结构是这样的

有其他问题可以评论联系,*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。

这篇关于vue2 增加左侧目录搜索功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1075251

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然