Using the 'white-space: pre-wrap' property does not function properly when sending text as a prop in Vue

In my Vue project, I'm passing a lengthy text as a prop to a child component.

Here is the code for the child component:

    <template>
      <div class="story">
        <p class="story__content">{{ content }}</p>
      </div>
    </template>

This is how it's used in the parent component:

             <story
                content="Before your trip, I recommend researching the destination to find the most interesting photo opportunities.\n I always research the location I’m traveling to.\n I do this extensively and obsessively!"
            />

I attempted to use CSS within the <p> tag of the child component like this:

.story__content {
    white-space: pre-wrap;
}

However, instead of displaying line breaks, the text appeared without any breaks like this: https://i.sstatic.net/60Pjv.png

Has anyone encountered a similar issue? Thank you in advance for any help!

Answer №1

To enhance your code, make sure to include the v-html directive in your paragraph tag. This allows the content prop to be interpreted as HTML:

<template>
  <div class="story">
    <p class="story__content" v-html="content" />
  </div>
</template>

For further information on this topic, check out this resource.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery click() function fires twice when dealing with dynamic elements

I am loading content from a database through ajax into a div and then when clicking on any of these content pieces, it should reload new content. The ajax request is initialized as a method within a class that I call at the beginning: function Request(ta ...

What is the method for adding the square root symbol to a button's value?

I am attempting to display the square root symbol inside a button, but I am having trouble making it work. Below is my button code: <input type="button" id="sqrt" value="sqrt" onclick="insert function here"> For the value, I want the square root sy ...

Directing users to varying pages based on a particular criteria

As we continue to develop our application, we are creating various pages and need to navigate between them. Our current framework is Next.js. The issue we are facing involves the Home page: when transitioning from the Home page to another page (such as pa ...

Checking if the upload process has been completed using XMLHttpRequest Level 2

Currently, I am utilizing ajax for uploading files. Once the file has been uploaded, PHP needs to conduct a thorough check on it (including mime type, size, virus scan using clamscan, and more). This process can take a few seconds, especially for larger fi ...

Properties that cannot be modified, known as read-only properties,

While browsing through this post on read-only properties, I stumbled upon the following code snippet: var myObject = { get readOnlyProperty() { return 42; } }; alert(myObject.readOnlyProperty); // 42 myObject.readOnlyProperty = 5; // Assignment al ...

Tips for sending a chosen value to a PHP controller

I've been attempting to send a selected value via ajax to a PHP controller, but unfortunately it's not functioning correctly. Below is my code snippet: function prodType() { $("#productType").change(function(){ let valu ...

`Can I embed inline .svg images into a Nuxt application?`

Is there a way to dynamically include an SVG HTML in a Vue Nuxt application and be able to style it? I tried creating a component but instead of the image, I am getting text data:image/svg+xhtml.... Any suggestions on how to resolve this issue? <templ ...

Trouble with initializing the Ionic map controller

I have a mobile app with 5 tabs, one of which features a map. However, the map only loads when directly accessed through the URL bar. It seems that the controller is not loaded when navigating to the map tab through the app, as indicated by console logs. S ...

Determine the availability of a distant website through AJAX requests

My website runs PHP's cURL to check the online status of 5 different URLs, however, this process tends to slow down the page load time significantly, especially if one of the sites being checked is not working. Someone suggested using jQuery's a ...

Do I require a Javascript framework, or can I achieve the desired functionality with the

During my time building Microsoft Excel apps using VBA, I worked with events like _Change and _Click. Transitioning to JavaScript and frameworks was a bit overwhelming due to the asynchronous nature of it all. Moving on to Python and Flask has been a ref ...

What is the best way to incorporate and manipulate data that is accessed from a Vue Template?

In my Vue project, I am utilizing the CDN method but now require the router functionality. Fortunately, I came across a tutorial that provides a clear explanation on how to set up routing without the need for CLI & Webpack. Check it out here: Following th ...

Express.js never terminates a session

I have a Backbone View that makes an Ajax call to the server to delete a session. Upon triggering the following event on the server: app.delete('/session', function(req, res) { if (req.session) { req.session.destroy(function() { ...

Tips on how to properly handle Promises in a constructor function

My Angular Service is currently making http requests, but I am looking to retrieve headers for these requests from a Promise. The current setup involves converting the promise to an Observable: export class SomeService { constructor(private http: HttpCl ...

Struggling to change h2 style on WordPress?

In a WordPress page, I create three heading texts and adjust the CSS when the screen width is less than 820px. Here is the code snippet: <h1 class="block-h1">Heading 1</h1> <h2 class="block-h2">Heading 2</h2> <h3 class="block-h3 ...

Retrieving the input value from embedded cells in a specific row of a table

I need the function to calculate the result of the following expression using values from the table below: a+b*c-(e/f) This calculation should exclude rows with e and f. Although I encountered a similar issue before, there was no solution for this spe ...

Utilizing Asp.Net for seamless file uploading with HTML5's drag and drop feature

Struggling to upload a file using HTML5's Drag and Drop (DnD) and File API. Unsure about how to send form data to the server, attempted using XMLHttpRequest but encountered issues. Here is what I have so far: <body> <form id="fo ...

Proper communication handling with child components in small React and Typescript setups

I am looking to design a NavBar component with NavBar.Item elements that will define the buttons within the navigation bar. Here is an example of what I have in mind: <NavBar> <NavBar.Item text="Home"/> <NavBar.Item text=&q ...

What is the best way to fill a single row with a solid color and no border?

It's table time! <table class="custom-table"> <tr> <td>231232131</td> <td>INTERNATIONAL FEDERATION O</td> </tr> </table> I'm aiming for a row with some color, li ...

Issue with Saving Query Loop Results in Prisma MySQL with NextJS/ReactJS

I'm struggling with a Prisma query that is giving me grief. Despite my best efforts, I can't figure out why it's not functioning properly. The getImages() method below is intended to retrieve all the images associated with a product. Query ...

After filtering the array in JavaScript, perform an additional process as a second step

My task involves manipulating an array through two methods in sequence: Filter the array Then, sort it The filter method I am using is as follows: filterArray(list){ return list.filter(item => !this.myCondition(item)); } The sort method I a ...