效果图
需要把换行符替换成p标签,方便首行缩进。主要方法使用正则进行转义
this.articleData = '<p>' + content.replace(/\n*$/g, '').replace(/\n/g, '</p> <p>') + '</p>'
代码示例:
<template>
<main>
<pre v-html="this.articleData" v-if="show"></pre>
<div class="page_">
<input type="submit" value="上一页">
<input type="submit" value="下一页" @click="nextPage()">
</div>
</main>
</template>
<sctipt>
// 获取文章内容
getArticle() {
this.$ajax.get(this.$novel_url + 'article', {
params: {
link: this.link,
n: this.page,
id: this.$route.query.id
}
}).then(data => {
let content = data.data.chapter.body;
this.articleData = '<p>' + content.replace(/\n*$/g, '').replace(/\n/g, '</p> <p>') + '</p>'
this.show = true;
this.ToastClear()
})
},
</script>
<style lang='less' scoped>
main {
padding: 0 10px;
margin-top: 50px;
text-align: left;
// line-height: 34px;
font-size: 16px;
width: calc(100% - 20px);
pre {
width: 100% !important;
text-indent: 2em;
text-align: left;
line-height: 34px;
white-space: inherit;
color: #2c3e50;
p {
line-height: 34px;
}
}
.page_{
width: 170px;
display: block;
margin: 15px auto;
input{
width: 80px;
background: #fff;
}
}
}
</style>