Vue Highlight Words
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
-
[Vue warn]: Failed to resolve component: Highlighter
cause The `Highlighter` component was not correctly imported or registered in your Vue application.fixEnsure `import Highlighter from 'vue-highlight-words'` is present in your script block and `Highlighter` is listed in the `components` option of your Vue component (e.g., `components: { Highlighter }`). -
TypeError: Cannot read properties of undefined (reading 'split') (or similar error related to array methods)
cause The `searchWords` prop expects an array of strings, but it received an undefined or non-array value.fixAlways pass an array of strings to the `searchWords` prop. If the input is from a string, ensure it's processed into an array (e.g., `this.words.split(' ')`) before binding. -
Property or method "text" is not defined on the instance but referenced during render
cause A data property or computed property referenced in the template (e.g., `text` or `keywords`) has not been correctly defined in the component's `data()` or `computed` options.fixVerify that all variables used in your template are properly declared in the `data()` return object or as `computed` properties within your Vue component's script block.
Warnings
- breaking Version 3.0.0 removed Babel transpilation for distribution code, which might affect older build setups expecting CommonJS output or specific transpilation targets. It also included a full TypeScript rewrite.
- breaking The `custom` prop was removed in v3.0.0. Custom rendering logic must now be implemented directly using slots.
- breaking Version 2.0.0 introduced breaking changes by dropping support for Vue 2, now exclusively supporting Vue 3.0.0 and newer. Additionally, `highlight tag props` were removed.
- gotcha This package is specifically for Vue 3. If you are developing with Vue 2, you must use a version from the `1.0` branch, which is separately maintained.
Install
-
npm install vue-highlight-words -
yarn add vue-highlight-words -
pnpm add vue-highlight-words
Imports
- Highlighter
import { Highlighter } from 'vue-highlight-words'import Highlighter from 'vue-highlight-words'
- Highlighter (CommonJS)
const Highlighter = require('vue-highlight-words')const { default: Highlighter } = require('vue-highlight-words') - Highlighter Component Type
import type Highlighter from 'vue-highlight-words'
Quickstart
<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>