2

In my Vue.JS code I have array in object which I parsed in string like this:

destination: item[1].destination.join('\n')

In console.log(obj) I can see it looks like this:

destination: "test1\ntest3\ntest3"

But on page it looks like common string, without \n.

enter image description here

How can I parse it to make HTML see those \n?

3
  • In order to display newlines in HTML you will need to use <br> Commented Dec 6, 2021 at 10:48
  • Do you want three separate list items or a single list item with three neested items or a single list item with a single element separated by new lines? Commented Dec 6, 2021 at 10:49
  • It looks like this - I have array with x elements and I need to do something to show it on page, but every record should be on a new line Commented Dec 6, 2021 at 10:53

2 Answers 2

1

You need to use style="white-space: pre-line" on your composant html

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

Comments

0

If you have an array of items then Try following the below approach

let app = new Vue({
 el: '#app',
 data() {
  return {
    textData: ['Text1\nText2\nText3', 'Text4\nText5\nText6']
  };
 },
 });
.sampleDiv {
  white-space: pre-line;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="sampleDiv" v-for="(text, index) in textData" :key="index">{{text}}</div>
</div>

1 Comment

The above solution applies for an array of items which you want to be listed on separate lines

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.