Vue Highlight Words

3.0.1 · active · verified Sun Apr 19

The `vue-highlight-words` library is a Vue component designed to efficiently highlight specified words within a larger body of text. It is currently stable at version 3.0.1, primarily supporting Vue 3.0.0 and above. The package has recently undergone a significant rewrite in TypeScript, as seen in its v3.0.0 release, indicating active maintenance and a commitment to modern web development standards. Its release cadence is tied to Vue's major versions, with v2.0.0 introducing Vue 3 support and v3.0.0 focusing on internal restructuring and type safety. A key differentiator of `vue-highlight-words` is its approach to rendering: unlike many alternatives, it utilizes Vue's `render` function directly instead of relying on `v-html` or `el.innerHTML`. This method significantly enhances security by preventing cross-site scripting (XSS) vulnerabilities often associated with raw HTML injection. The component is a direct port of the popular `react-highlight-words` library, offering a similar API and feature set for Vue developers. It provides options for custom styling, active highlighting, and auto-escaping search terms, making it a robust solution for text highlighting needs within Vue applications.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to import and use the `Highlighter` component within a Vue 3 Single File Component (SFC), providing text to highlight and an array of search terms.

<template>
  <div id="app">
    <Highlighter class="my-highlight" :style="{ color: 'red' }"
      highlightClassName="highlight"
      :searchWords="keywords"
      :autoEscape="true"
      :textToHighlight="text"/>
  </div>
</template>

<script>
import Highlighter from 'vue-highlight-words'

export default {
  name: 'app',
  components: {
    Highlighter
  },
  data() {
    return {
      text: 'The dog is chasing the cat. Or perhaps they\'re just playing?',
      words: 'and or the'
    }
  },
  computed: {
    keywords() {
      return this.words.split(' ')
    }
  }
}
</script>

view raw JSON →