As you all know, in HTML, if your content contains \n, it will wrap, but in applets, if you write in view tags, it will not wrap, so what can you do? It’s easy, applets only have two tags, and since view doesn’t work, we can use text
<view> line 1 \n line 2 </view> // line 1 \n line 2 <text> line 1 \n line 2 </text> // line 1 // line 2Copy the code
But, do you think it is so simple, this is a small program, there will be pits in it (dog head
Yes, I happened to see it. Recently, I was making an active page. The active rule in the page is returned from the interface, and it looks like this:
{data: {rule: '1. Rule 1 \n2. rule 2 '}, status: 200}Copy the code
After a basic assignment, the page displays, but whether I use the View tag or the text tag, the page displays a 1. Rule # 1 \n2. Rule # 2: why is data fetched from the interface invalid, while data written dead on the page valid?
After some reflection, I think that if I get it from the interface, I will treat it as a whole string. I will use filter to process it and convert \n (this small program is based on uni-app framework, so there is filter. If I write it in native form, I will use other methods).
<view>{{rule | formatRule}}</view>
filters: {
formatRule(val) {
return val.raplace(/\\n/g,'\n')
}
}
Copy the code
Finished ~