vue-nl2br is a Vue.js component designed to automatically convert newline characters (`\n`) within a string prop into HTML `<br>` tags, effectively rendering multiline text with line breaks in the browser. It explicitly targets Vue 2 (`^2.0.0`) environments. The current stable version is 1.1.1, with the last known update in August 2021, indicating a very slow release cadence, if any, for future feature development. A key differentiator is its explicit discussion against simply using CSS `white-space: pre;`, suggesting it's intended for scenarios where semantic `<br>` tags are preferred or required over CSS-driven formatting, or when the `white-space` property causes unwanted side effects. It provides options for both global and local component registration and supports custom HTML tags and class names for the wrapping element.
npm install vue-nl2brVerified import paths — ran on the pinned version, not inferred.
Demonstrates local component registration and usage of the `nl2br` component with `tag`, `text`, and `class-name` props, including dynamic text updates and `v-if` for empty states.
For Vue 3 projects, consider creating a simple custom component or using CSS `white-space: pre-wrap;` as an alternative. If semantic `<br>` tags are strictly required, a custom Vue 3 component would be necessary.
Use `<nl2br v-if="myText" tag="p" :text="myText" />` where `myText` is the variable holding your content.
Evaluate your use case: if the line breaks are purely for visual presentation, `white-space: pre-wrap;` might be a simpler and more performant solution. If semantic line breaks are critical, `vue-nl2br` is appropriate.
Ensure you either call `Vue.component('nl2br', Nl2br)` for global registration (typically in your main.js/ts) or include `Nl2br` in the `components` option of your parent Vue component for local registration.Always provide the `text` prop to the `nl2br` component, even if it's an empty string or `null` (unless using `v-if` to conditionally render).