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

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

Python如何实现高效的文件/目录比较

《Python如何实现高效的文件/目录比较》在系统维护、数据同步或版本控制场景中,我们经常需要比较两个目录的差异,本文将分享一下如何用Python实现高效的文件/目录比较,并灵活处理排除规则,希望对大... 目录案例一:基础目录比较与排除实现案例二:高性能大文件比较案例三:跨平台路径处理案例四:可视化差异报

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?