实现方案:用正则全局替换
// 避免搜索特殊字符,如“\”时报错,对value进行转义
const regName = new RegExp(`\\${value}`, 'g');
const itemLists = this.indexTreeDataLists.filter(o => o.name.match(regName));
if (itemLists.length) {
itemLists.forEach(item => {
const result = [];
this.helpGetSearchName(item.parent, result);
let resultStr = `${result.join('/')}${item.parent ? '/' : ''}${item.name}`;
// 替换搜索结果
resultStr = resultStr.replace(regName, `<span style="color:#0090FF;">${value}</span>`);
}
// 帮助 - 搜索 - 拼接搜索结果
helpGetSearchName(data, result = []) {
if (!data) return;
if (data.parent) {
this.helpGetSearchName(data.parent, result);
}
result.push(data.name);
},