2

In a response from an API I get a plain text that contains line breaks in this way:

"plain_text": "INFORMACION DEL PRODUCTO:\n\nProducto:

How can I use these line breaks (\n\n) when printing on WEB page with Javascript (I'm using VUE) ?

Since it only passes the text without the line breaks.

I add image of how it prints with VUE but in the inspector if it appears with the line breaks.

text

8
  • could you use a <br> tag? Commented Dec 3, 2018 at 5:08
  • Yes but I get these line breaks from the API call Commented Dec 3, 2018 at 5:09
  • @admcfajn you mean to use replace? Commented Dec 3, 2018 at 5:11
  • 1
    I think vue will not admit that. Commented Dec 3, 2018 at 5:14
  • 1
    @eag845 It does not work with VUE as you say Commented Dec 3, 2018 at 5:18

2 Answers 2

10

Use the CSS white-space property...

const res = {"plain_text": "\t\t\t\tINFORMACION DEL PRODUCTO:\n\nProducto:"}
new Vue({
  el: '#app',
  data: { res }
})
.pre-formatted {
  white-space: pre-wrap; /* 👈 this is the important part */
  border: 1px dotted;
  padding: 1rem;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<div id="app">
  <p class="pre-formatted">{{res.plain_text}}</p>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

@OscarDev I just changed it to pre-wrap based on a closer look at your images. This keeps the indents as they appear without collapsing them into a single space character
-1

In html white-spaces are compressed into a single space. Check this for a similar post.

To have line-breaks in rendered html, you have to manually replace the '\n' with <br/> tags.

Comments

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.