当前位置:首页 > VUE

vue实现高亮显示

2026-02-20 06:02:24VUE

Vue 实现高亮显示的方法

使用 v-html 指令

在 Vue 中可以使用 v-html 指令动态渲染 HTML 内容。通过将需要高亮的文本替换为带有样式标签的字符串,可以实现高亮效果。

<template>
  <div v-html="highlightedText"></div>
</template>

<script>
export default {
  data() {
    return {
      originalText: 'This is some text with keywords to highlight',
      keyword: 'keywords'
    }
  },
  computed: {
    highlightedText() {
      return this.originalText.replace(
        new RegExp(this.keyword, 'gi'),
        match => `<span style="background-color: yellow">${match}</span>`
      )
    }
  }
}
</script>

使用自定义指令

可以创建一个自定义指令专门处理文本高亮逻辑,使代码更模块化。

<template>
  <div v-highlight="keyword">{{ originalText }}</div>
</template>

<script>
export default {
  data() {
    return {
      originalText: 'This is another example with highlighted words',
      keyword: 'highlighted'
    }
  },
  directives: {
    highlight: {
      inserted(el, binding) {
        const regex = new RegExp(binding.value, 'gi')
        el.innerHTML = el.textContent.replace(
          regex,
          match => `<span style="background-color: #ffeb3b">${match}</span>`
        )
      }
    }
  }
}
</script>

使用组件封装

创建一个可重用的高亮组件,方便在多个地方使用。

<template>
  <highlight :text="text" :query="searchQuery"></highlight>
</template>

<script>
import Highlight from './Highlight.vue'

export default {
  components: { Highlight },
  data() {
    return {
      text: 'Component-based highlighting example',
      searchQuery: 'highlighting'
    }
  }
}
</script>

Highlight.vue 组件实现:

<template>
  <div v-html="processedText"></div>
</template>

<script>
export default {
  props: ['text', 'query'],
  computed: {
    processedText() {
      if (!this.query) return this.text
      const regex = new RegExp(this.query, 'gi')
      return this.text.replace(
        regex,
        match => `<span class="highlight">${match}</span>`
      )
    }
  }
}
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

使用第三方库

可以考虑使用专门的高亮库如 mark.js 来实现更复杂的高亮功能。

安装 mark.js:

npm install mark.js

使用示例:

vue实现高亮显示

<template>
  <div ref="content">
    {{ content }}
  </div>
</template>

<script>
import Mark from 'mark.js'

export default {
  data() {
    return {
      content: 'Text content to be highlighted goes here',
      searchTerm: 'highlighted'
    }
  },
  mounted() {
    this.highlightText()
  },
  methods: {
    highlightText() {
      const instance = new Mark(this.$refs.content)
      instance.mark(this.searchTerm, {
        className: 'custom-highlight'
      })
    }
  }
}
</script>

<style>
.custom-highlight {
  background-color: #ffeb3b;
  padding: 0 2px;
}
</style>

注意事项

  • 使用 v-html 时要注意 XSS 攻击风险,确保内容可信
  • 高亮逻辑区分大小写时需调整正则表达式标志
  • 对于大量文本的高亮操作,考虑性能优化
  • 可以扩展高亮样式,添加动画或其他视觉效果

以上方法可以根据具体需求选择或组合使用,实现灵活多样的文本高亮效果。

标签: vue高亮
分享给朋友:

相关文章

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现新闻

vue实现新闻

Vue 实现新闻功能 使用 Vue 实现新闻功能通常涉及新闻列表展示、新闻详情页、分类筛选等模块。以下是实现的基本思路和代码示例。 新闻列表展示 通过 Vue 组件展示新闻列表,通常使用 v-for…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…