In vue, the / n line break is not recognized in the tag, looking for a solution

the data obtained by textarea in vue is passed to the backend. When I get the details, I use the API to get the data I passed in earlier.
but the data I got is put in the tag, the / n newline character is not recognized, and it is still displayed in plain text

.

Printing to the console is normal with line feeds, but it is not available on the page. The effect is shown in figure

.

clipboard.png

v-html


console.log(data.job_description)

clipboard.png

they all change lines normally, so ask for advice on why.

Oct.21,2021

your /\\ n/gm rule represents the global multiline matching string \ n
\ represents escape characters in the rule.
\ n represents a carriage return line feed
\ n represents a string\ n

correct should be as follows

data.job_description.replace(/\n/gm,"<br/>")

just change the rule;

this.text = data.job_description.replace(/\n/g,"<br/>");

I have recently encountered the same problem and found a solution that can be solved through css

the first add style

<p style="white-space: pre-wrap;" v-html="content" >

the second way to add pre tags

<p v-html="content">

original link


you don't render and click the dom element to see if the br tag replacement failed to

var reg= / [n] / try to write


this.text = data.job_description.replace (/\ nUniGM, "< br/ >")
console.log (this.text)
output this.text! Data.job_description is unchanged

Menu