这种情况一般用于回显的时候,在文本框汇总输入换行,然后得到的代码也要是换行的(默认是中间有空格的)
方法一:可以用replace替换空格和换行
this.temp.content = this.temp.content.replace(/\n/g, '<br/>')
this.temp.content = this.temp.content.replace(new RegExp(' ', 'gm'), ' ')
方法二:识别换行后 分别加上p标签
这里需要注意的是 在传给接口前还需要将添加p标签后的数组形式转换成字符串
this.temp.content = this.temp.content.replace(new RegExp(' ', 'gm'), ' ') // gm 全局替换
const arr = []
this.temp.content.split('\n').forEach(item => {
arr.push(`<p>${item.trim()}</p>`)
})
this.temp.content = arr.join('')