基于vue远程搜索、模糊查询组件
<template>
<div class="Home">
<input
v-model="keyWords"
type="text"
placeholder="请输入关键词"
@input="handleQuery"
/>
<ul>
<li v-for="(item, index) in results" :key="index" v-html="item.name" @click="getText(item.name)"></li>
</ul>
</div>
</template>
<script>
export default {
name: 'Home',
data() {
return {
keyWords: '',
results: [],
timer:null,
FalseData:[
{name:'他的名字叫张三'},
{name:'张三是他的名字'},
{name:'张三就是他本人'},
] //假数据
}
},
methods: {
clearTimer() {
if (this.timer) {
clearTimeout(this.timer)
}
},
handleQuery(event) {
this.clearTimer()
console.log(event.timeStamp)
this.timer = setTimeout(() => {
console.log(event.timeStamp)
// console.log(this.lastTime)
// if (this.lastTime - event.timeStamp === 0) {
// this.$http.post('/api/vehicle').then(res => {
// console.log(res.data.data)
// this.changeColor(res.data.data)
// })
this.changeColor(this.FalseData)
// }
}, 2000)
},
changeColor(resultsList) {
resultsList.map((item, index) => {
// console.log('item', item)
if (this.keyWords && this.keyWords.length > 0) {
// 匹配关键字正则
let replaceReg = new RegExp(this.keyWords, 'g')
// 高亮替换v-html值
let replaceString =
'<span class="search-text">' + this.keyWords + '</span>'
resultsList[index].name = item.name.replace(
replaceReg,
replaceString
)
}
})
this.results = []
this.results = resultsList
},
// 把带有html 的文本变成text文本
repalceHtmlToText(str) {
str = str.replace(/<\/?.+?>/g, "");
str = str.replace(/ /g, "");
return str;
},
// 得到转化后的文本赋值给 keyWords
getText(name){
console.log(name)
this.keyWords = this.repalceHtmlToText(name)
this.$emit("getName",name)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.search-text {
color: red;
}
</style>
参考https://blog.csdn.net/weixin_34399060/article/details/88771087