vue精准查询,模糊查询,输入西天,可以查询到西安的天气很好,输入我的,可以查询到我是你的宝,输入的关键字,西天,我的高亮显示
时间: 2023-12-06 07:02:33 浏览: 23
您可以使用Vue.js的computed属性来实现精准查询和模糊查询,并使用v-html指令来实现关键字高亮显示。
首先,您需要定义一个data属性来存储您的查询结果和关键字:
```
data() {
return {
keyword: '',
result: ''
}
}
```
然后,您可以使用computed属性来实现精准查询和模糊查询:
```
computed: {
filteredList() {
if (this.keyword) {
// 精准查询
return this.weatherList.filter(item => item.city === this.keyword)
} else {
// 模糊查询
return this.weatherList.filter(item => item.city.includes(this.keyword))
}
}
}
```
接下来,您可以在模板中使用v-for指令来显示查询结果,并使用v-html指令来实现关键字高亮显示:
```
<div v-for="item in filteredList" :key="item.city">
<p v-html="item.city.replace(keyword, '<span class=\'highlight\'>' + keyword + '</span>')"></p>
<p v-html="item.weather.replace(keyword, '<span class=\'highlight\'>' + keyword + '</span>')"></p>
</div>
```
最后,为了实现关键字高亮显示的效果,您需要在样式表中定义.highlight类:
```
.highlight {
color: red;
font-weight: bold;
}
```
通过以上的方法,您就可以实现精准查询、模糊查询和关键字高亮显示的功能了。