1、npm安装 fuse.js:
npm install --save fuse.js
2、参考代码和用法:
<template>
<div class="header-search" :class="{ show: true }">
<!-- <svg-icons icon-class="index" class-name="search-icon"/> -->
<el-select
id="operationGuideNavbarSearch"
ref="headerSearchSelect"
v-model="search"
:remote-method="querySearch"
filterable
default-first-option
remote
placeholder="搜索名称"
class="header-search-select"
@change="change"
>
<el-option
v-for="option in options"
:key="option.item.path"
:value="option.item"
:label="option.item.title.join(' > ')"
/>
<img
class="search-icon"
slot="prefix"
src="@/icons/svg/power-search.svg"
/>
</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/dist/fuse.min.js";
import path from "path";
export default {
name: "HeaderSearch",
data() {
return {
search: "",
options: [],
searchPool: [],
show: true,
fuse: undefined,
};
},
computed: {
routes() {
return this.$store.getters.permission_routes;
},
},
watch: {
// $route() {
// this.search = ""; //重置搜索结果
// },
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.show;
if (this.show) {
this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus();
}
},
close() {
this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur();
this.options = [];
this.show = false;
},
change(val) {
// console.log(this.options);
const path = val.path;
if (this.ishttp(val.path)) {
// http(s):// 路径新窗口打开
const pindex = path.indexOf("http");
window.open(path.substr(pindex, path.length), "_blank");
} else {
this.$router.push(val.path);
}
// console.log(val);
this.search = ""; //重置搜索结果
this.options = [];
this.$nextTick(() => {
// 显示搜索结果
// this.search = val.title[0] + " > " + val.title[1];
this.show = false;
});
},
initFuse(list) {
this.fuse = new Fuse(list, {
shouldSort: true, // 是否按分数对结果列表排序
includeScore:true, // 是否应将分数包含在结果集中。0分表示完全匹配,1分表示完全不匹配。
threshold: 0.1, // 匹配算法阈值。阈值为0.0需要完全匹配(字母和位置),阈值为1.0将匹配任何内容。
location: 0, // 确定文本中预期找到的模式的大致位置。
distance: 100,
maxPatternLength: 32, // 模式的最大长度
minMatchCharLength: 1, // 模式的最小字符长度
keys: [
{
name: "title",
weight: 0.7,
},
{
name: "path",
weight: 0.3,
},
], // 搜索标题与路径
});
},
// Filter out the routes that can be displayed in the sidebar
// And generate the internationalized title
generateRoutes(routes, basePath = "/", prefixTitle = []) {
let res = [];
for (const router of routes) {
// skip hidden router
if (router.hidden) {
continue;
}
const data = {
path: !this.ishttp(router.path)
? path.resolve(basePath, router.path)
: router.path,
title: [...prefixTitle],
};
if (router.meta && router.meta.title) {
data.title = [...data.title, router.meta.title];
if (router.redirect !== "noRedirect") {
// only push the routes with title
// special case: need to exclude parent router without redirect
res.push(data);
}
}
// recursive child routes
if (router.children) {
const tempRoutes = this.generateRoutes(
router.children,
data.path,
data.title
);
if (tempRoutes.length >= 1) {
res = [...res, ...tempRoutes];
}
}
}
return res;
},
querySearch(query) {
// console.log("query", query);
if (query !== "") {
this.options = this.fuse.search(query);
} else {
this.options = [];
}
},
ishttp(url) {
return url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1;
},
},
};
</script>
3、可以参考其他链接:
(精华)2020年7月16日 vue Fuse.js模糊搜索引擎_愚公搬代码的博客-CSDN博客_vue-fuse<template> <div class="hello"> <el-input v-model="title" placeholder="请输入内容"></el-input> <ul> <li v-for="item in result"> 标题: {{item.item.title}} <br /> 作者: {{item.item.authohttps://blog.csdn.net/aa2528877987/article/details/107378864/