小程序,对返回的字符串进行分段且每行实现首行缩进

<template>
    <!-- 文字分页代码,前提是必须得有一个分段标识,本页中是通过@进行标识 -->
    <rich-text :nodes="nodes"></rich-text>
</template>

<script>
export default {
  data() {
    return {
      nodes: []
    }
  },
  onLoad() {
    // 假设后端返回的文字为text
    const text = "这是第一段文字@这是第二段文字@这是第三段文字";
    const paragraphs = text.split("@");
    const nodes = [];
    // 循环处理每个段落
    paragraphs.forEach(paragraph => {
      nodes.push({
        name: "p",
        attrs: {
          style: "text-indent: 2em;"
        },
        children: [{
          type: "text",
          text: paragraph
        }]
      });
    });
    this.nodes = nodes;
    console.log(this.nodes);
  }
}
</script>

<style lang="scss">
.img_box {
    margin: 100upx;
    width: 400upx;
    height: 200upx;
    border: 2upx solid orange;
}
</style>

猜你喜欢

转载自blog.csdn.net/B1841630/article/details/129726001
今日推荐