/*
联想建议
*/
<template>
<div class="searchSuggestionMain">
<van-cell v-for="(item, index) in suggestionsList" :key="index" icon="search" clickable>
<div slot="title" v-html="heightlight(item.content)"></div>
</van-cell>
<!-- {{}}绑定=>纯文本Hello <span style="color:red">World</span> -->
<div>{{ htmlStr }}</div>
<!-- v-html指令 -->
<div v-html="htmlStr"></div>
</div>
</template>
<script>
export default {
name: 'SearchSuggestion',
props: {
searchText: {
type: String,
required: true
}
},
data() {
return {
suggestionsList: [],
htmlStr: 'Hello World'
}
},
methods: {
// 高亮函数
heightlight(text) {
// 动态变量创建正则
/*
RegExp正则构造函数
参数一:匹配模式字符串
参数二:匹配模式,gi全局忽略大小写
*/
const reg = new RegExp(this.searchText, 'gi')
return text.replace(reg, `<span class="active">${this.searchText}</span>`)
},
}
}
</script>
<style scoped lang="less">
/deep/ span.active {
color:
}
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54