Vue Linkify Directive
The `vue-linkify` package provides a simple Vue.js directive (`v-linkified`) designed to automatically convert URLs and email addresses within plain text into clickable HTML `<a>` links. This package is built upon the core `linkifyjs` library. Last updated in December 2016 with version 1.0.1, it is primarily compatible with Vue.js 2.x applications and is considered abandoned. Due to its age, it does not support Vue 3.x and incorporates an older version of `linkifyjs`, lacking features and breaking changes introduced in more recent `linkifyjs` releases. Newer alternatives exist for Vue 3 environments.
Common errors
-
Error: Property "directive" must be a function or an object with a "get" and/or "set" method. Did you register a Vue 2.x directive in a Vue 3.x app?
cause Attempting to use `vue-linkify` (a Vue 2 directive) in a Vue 3 application, where the directive registration API has changed.fixThis package is for Vue 2.x. For Vue 3, you must use a Vue 3-compatible linkify directive, such as `vue-3-linkify` or `v-linkify`. Register it using `app.directive()` after creating the Vue app instance.
Warnings
- breaking This package is not compatible with Vue 3.x. Vue 3 introduced breaking changes to the directive registration API. You will need to use a different, actively maintained `linkify` directive for Vue 3.
- gotcha The `vue-linkify` package is very old (last updated 2016) and uses an outdated version of `linkifyjs`. It will not include features, performance improvements, or bug fixes present in modern `linkifyjs` versions (e.g., v3.0.0+).
- gotcha Using `v-html` along with `v-linkified` can pose security risks if the `raw` content is not sanitized before being passed to `v-html`. Malicious scripts embedded in `raw` content could be executed.
- gotcha The default behavior of `linkifyjs` (which `vue-linkify` is based on) in older versions might differ from modern expectations regarding `target="_blank"` and `rel="noopener noreferrer"` attributes for security. Newer `linkifyjs` versions removed these defaults.
Install
-
npm install vue-linkify -
yarn add vue-linkify -
pnpm add vue-linkify
Imports
- linkify
const linkify = require('vue-linkify')import linkify from 'vue-linkify'
- Vue.directive
app.directive('linkified', linkify)Vue.directive('linkified', linkify) - v-linkified
<div v-html="raw" v-linkified />
Quickstart
import Vue from 'vue';
import linkify from 'vue-linkify';
Vue.directive('linkified', linkify);
new Vue({
el: '#app',
data: {
raw: 'Hello from vuejs.org. My email is user@example.com and phone is +1-555-LINK-ME.'
},
template: `
<div id="app">
<p>Original text:</p>
<p>{{ raw }}</p>
<p>Linkified text:</p>
<div v-html="raw" v-linkified:options="{ className: 'my-link', target: '_blank' }" />
</div>
`
});