Vue Newline to Line Break Component

1.1.1 · maintenance · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

<!-- MyComponent.vue -->
<template>
  <div>
    <h1>User Generated Content</h1>
    <nl2br 
      v-if="longText" 
      tag="p" 
      :text="longText"
      class-name="content-paragraph"
    />
    <p v-else>No content to display.</p>
    
    <h2>Example with multiple lines:</h2>
    <nl2br tag="div" :text="`This is the first line.\nThis is the second line.\nAnd a final line.`" />
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import Nl2br from 'vue-nl2br';

export default Vue.extend({
  name: 'MyComponent',
  components: {
    Nl2br,
  },
  data() {
    return {
      longText: 'Hello world!\nThis is some text that should\nbreak into multiple lines.',
    };
  },
  mounted() {
    // Simulate dynamic text loading
    setTimeout(() => {
      this.longText = 'Updated text after a delay.\nIt still respects newlines.\nEnjoy!';
    }, 2000);
  }
});
</script>

<style scoped>
.content-paragraph {
  font-family: sans-serif;
  color: #333;
  line-height: 1.5;
}
</style>

view raw JSON →