vue处理后端返回的日志

vue处理后端返回的日志,并保持日志内容最新(滚动到最新内容)
1、后端返回的日志格式如下所示,该如何处理成正常的文本换行
在这里插入图片描述

2、在获取日志的接口中做如下处理,把返回的/n替换成换行标签,并根据任务状态判断日志是否需要轮询

getLogInfo(id) {
   this.$axios
     .get(`/ai/train/logs/${id}`)
      .then((res) => {
          if (res.data.code == 200) {
              this.logText = "";
              if (res.data.data) {
                 res.data.data.forEach((item) => {
                    this.logText += item.replace(/\n/g, "<br/>");
                 });
              if ( this.modelInfo.status == 0 ) {
                this.startPolling();
              } else {
                this.endPolling();
              }              
              this.scrollToBottom();
         } else {
           this.logText = "暂无日志信息";
         }
    }
})

3、在html中使用,为标签设置v-html

<div class="log-box-content-text" v-html="logText" id="logCon"></div>

4、轮询日志,一般日志是需要实时更新的,所以需要根据任务状态轮询日志

startPolling() {
    this.pollTimer = setTimeout(() => {
        this.getLogInfo(this.id);
    }, 5000);
},
endPolling() {
    clearInterval(this.pollTimer);
},

5、滚动条保持最底部方法,在获取日志的接口调用

    scrollToBottom() {
      this.$nextTick(() => {
        var container = document.querySelector("#logCon");
        container.scrollTop = container.scrollHeight;
      });
    },

轮询相关资源:vue中数据状态轮询

相关推荐

  1. DataV轮播表返回数据处理

    2024-04-04 10:40:06       40 阅读
  2. 前端处理axios请求下载返回文件流

    2024-04-04 10:40:06       35 阅读
  3. 下载返回二进制文件

    2024-04-04 10:40:06       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-04 10:40:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-04 10:40:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-04 10:40:06       87 阅读
  4. Python语言-面向对象

    2024-04-04 10:40:06       96 阅读

热门阅读

  1. 路由的hash和history模式的区别

    2024-04-04 10:40:06       36 阅读
  2. nvm 安装多个版本的Node npm

    2024-04-04 10:40:06       34 阅读
  3. 如何设计一个类似Dubbo的RPC框架

    2024-04-04 10:40:06       32 阅读
  4. 外汇MT4交易心得分享:如何规避常见交易陷阱?

    2024-04-04 10:40:06       34 阅读
  5. Docker客户端命令

    2024-04-04 10:40:06       42 阅读
  6. openharmony launcher 调研笔记 01

    2024-04-04 10:40:06       39 阅读
  7. Tomcat 启动闪退问题解决方法

    2024-04-04 10:40:06       36 阅读
  8. Kafka中groupid和auto.offset.reset的关系

    2024-04-04 10:40:06       38 阅读
  9. 谈谈Python中的ORM框架,如SQLAlchemy

    2024-04-04 10:40:06       38 阅读