0

I want to store linebreak in String variable and show as below(please ignore the image): enter image description here

I tried to add <br/> in text but not working.

App.vue

<template>
  <div id="app">{{ text }}</div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text:
        "Lorem ipsum is a pseudo-Latin text used in web design, typography, layout, and printing in place of English to emphasise design elements over content. It's also called placeholder (or filler) text. It's a convenient tool for mock-ups. It helps to outline the visual elements of a document or presentation, eg typography, font, or layout. Lorem ipsum is mostly a part of a Latin text by the classical author and philosopher Cicero. Its words and letters have been changed by addition or removal, so to deliberately render its content nonsensical; it's not genuine, correct, or comprehensible Latin anymore. While lorem ipsum's still resembles classical Latin, it actually has no meaning whatsoever. As Cicero's text doesn't contain the letters K, W, or Z, alien to latin, these, and others are often inserted randomly to mimic the typographic appearence of European languages, as are digraphs not to be found in the original.In a professional context it often happens that private or corporate clients corder a publication to be made and presented with the actual content still not being ready. Think of a news blog that's filled with content hourly on the day of going live. However, reviewers tend to be distracted by comprehensible content, say, a random text copied from a newspaper or the internet. The are likely to focus on the text, disregarding the layout and its elements. Besides, random text risks to be unintendedly humorous or offensive, an unacceptable risk in corporate environments. Lorem ipsum and its many variants have been employed since the early 1960ies, and quite likely since the sixteenth century.",
    };
  },
};
</script>

Codesandbox
https://codesandbox.io/s/friendly-sound-r8rql

2
  • stackoverflow.com/questions/58340993/… this answer will help Commented Oct 21, 2021 at 9:11
  • @NaKib Using v-html might be unsafe, and isn't necessarily needed here. Commented Oct 21, 2021 at 9:13

2 Answers 2

2

This is applicable to regular javascript as well:

const myString = "First line \nSecond line"

document.querySelector("#my-div").innerText = myString
<div id="my-div" style="white-space: pre-wrap;"></div>

The keywords here are:

  1. Use css-property white-space: pre-line (or similar)
  2. Add a \n to the string, as a newline.
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use the html directive if you want to include html:

<template>
  <div v-html="text"></div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text:
        "Lorem ipsum is a pseudo-Latin text used in web design, typography, layout, and printing in place of English to emphasise design elements over content. It's also called placeholder (or filler) text. It's a convenient tool for mock-ups. It helps to outline the visual elements of a document or presentation, eg typography, font, or layout. Lorem ipsum is mostly a part of a Latin text by the classical author and philosopher Cicero. Its words and letters have been changed by addition or removal, so to deliberately render its content nonsensical; it's not genuine, correct, or comprehensible Latin anymore. While lorem ipsum's still resembles classical Latin, it actually has no meaning whatsoever. As Cicero's text doesn't contain the letters K, W, or Z, alien to latin, these, and others are often inserted randomly to mimic the typographic appearence of European languages, as are digraphs not to be found in the original.<br>In a professional context it often happens that private or corporate clients corder a publication to be made and presented with the actual content still not being ready. Think of a news blog that's filled with content hourly on the day of going live. However, reviewers tend to be distracted by comprehensible content, say, a random text copied from a newspaper or the internet. The are likely to focus on the text, disregarding the layout and its elements. Besides, random text risks to be unintendedly humorous or offensive, an unacceptable risk in corporate environments. Lorem ipsum and its many variants have been employed since the early 1960ies, and quite likely since the sixteenth century.",
    };
  },
};
</script>

Example: https://codesandbox.io/s/wandering-snowflake-yod2x

1 Comment

Note that using v-html is a generally a big nono, and should be avoided if possible (and it's possible here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.