关键字高亮

215 阅读1分钟

补充高亮辅助函数

// 源字符串,要高亮的片段
export const heightLight = (str, key) => {
  const reg = new RegExp(key, 'ig')
  return str.replace(reg, (val) => {
    return `<span style="color:red">${val}</span>`
  })
}

添加计算属性

computed: {
  cSuggestions () {
      // this.suggestions中的每一项都去做替换
      // suggestions中的每一项   ====> heightLight(suggestions中的每一项, this.keyword)
      return this.suggestions.map(item => {
        return heightLight(item, this.keyword)
      })
    }
},

用v-for对计算属性进行循环

<!-- 2. 搜索建议