vue怎么处理后端返回的换行符'\n'

解决方案有三个,用第三个方案会好一点

  • 1、使用v-html的标签
1
2
3
4
5
6
7
<p class="text" v-html="requestText"></p>
data(){
return{
requestText:""
}
}
str.replace('ln', '<br>');
  • 2、使用v-for循环显示替换后的文本
1
<div v-for="line in str.split('\n')">{{line}}<br></div>
  • 3、css加上
1
2
white-space: pre-line;//多个空格会合并成一个空格
white-space: pre-wrap;//多个空格仍然不变

注意:
popupConfig.tip.replace(/\n/g, ‘\n’)
popupConfig.tip.replace(/\n/g, ‘
‘)