【问题标题】:Render [object HTMLTableElement] in VueJS在 VueJS 中渲染 [object HTMLTableElement]
【发布时间】:2018-05-31 03:09:04
【问题描述】:

那些尝试过jsdifflib的人知道这个插件返回一个HTMLTableElement。现在我想尝试在我的 VueJS 组件上渲染/显示它。

我尝试了以下方法:

模板

<div class="diff-container" v-html="dynamicHtmlContent" ref="auditContainer"></div>

组件

export default {
    name: 'AuditView',
    data() {
      return {
        dynamicHtmlContent: null
      }
    },
    created() {
      // code logic here and there
      this.processDataDiff(results, 0);
    },
    methods: {
      processDataDiff: function (data, index) {
        // code logic here and there
        this.displayDiff(
          JSON.stringify(object1, null, 4).replace(/\\n/g, '\n'),
          JSON.stringify(object2, null, 4).replace(/\\n/g, '\n'),
          versionId1,
          versionId2
        );
      },
      displayDiff: function (baseDoc, newDoc, baseVersion, newVersion) {
        this.dynamicHtmlContent = auditService.getDiff(baseDoc, newDoc, baseVersion, newVersion);
      }
    }
}

ES6 服务

 getDiff(baseTextRaw, newTextRaw, baseVersion, nextVersion) {
   // build the diff view and return a DOM node
   return difflib.buildView({
     baseText: baseTextRaw,
     newText: newTextRaw,
     // set the display titles for each resource
     baseTextName: 'Version ' + baseVersion,
     newTextName: 'Version ' + nextVersion,
     contextSize: 10,
     // set inine to true if you want inline
     // rather than side by side diff
     inline: false
   });
 }

我已经跳过了代码逻辑,但我已经检查了 dynamicHtmlContent 并返回为 HTML 对象,如下面的屏幕截图所示:

不知何故,使用 v-html 是不可能的,因为它只呈现一个对象 {},如 console.log(typeof this.dynamicHtmlContent); 上所说那么我如何将它呈现给我的 Vue 组件?我也发现这很难转换成纯字符串。请帮我解决这个问题。

【问题讨论】:

    标签: javascript vuejs2 html-object


    【解决方案1】:

    您仍然可以使用 v-html,您只需更改您正在访问的内容。由于您得到的最终将成为一个实际的 DOM 元素,因此您可以做几件事。

    首先是简单地改变 v-html 来访问你元素的outerHTML property

    v-html="dynamicHtmlContent.outerHTML"
    

    或者将outerHTML直接保存到dynamicHtmlContent而不是DOM元素

    this.dynamicHtmlContent = auditService.getDiff().outerHTML
    

    你可以做的另一件事是通过accessing your auditContainer reference through this.$refs直接附加DOM元素

    displayDiff: function (baseDoc, newDoc, baseVersion, newVersion) {
      var table = auditService.getDiff(baseDoc, newDoc, baseVersion, newVersion);
      this.$refs.auditContainer.appendChild( table );
    }
    

    请注意,这必须在组件安装后完成,因为 auditContainer 尚未创建。含义:

    created() {
      // code logic here and there
      this.processDataDiff(results, 0);
    },
    

    将改为:

    mounted() {
      // code logic here and there
      this.processDataDiff(results, 0);
    },
    

    v-html 演示

    var table = document.createElement('table');
    var body = table.createTBody();
    var row = body.insertRow();
    var cell = row.insertCell();
    cell.innerHTML = "A test table";
    
    var vm = new Vue({
      el: '#app',
      data() {
        return {
          dynamicHtmlContent: null
        }
      },
      created() {
        this.displayDiff();
      },
      methods: {
        displayDiff: function() {
          this.dynamicHtmlContent = table;
        }
      }
    });
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <div id="app">
      <div class="diff-container" v-html="dynamicHtmlContent.outerHTML" ref="auditContainer"></div>
    </div>

    DOM 附加演示

    var table = document.createElement('table');
    var body = table.createTBody();
    var row = body.insertRow();
    var cell = row.insertCell();
    cell.innerHTML = "A test table";
    
    var vm = new Vue({
      el: '#app',
      data() {
        return {
          dynamicHtmlContent: null
        }
      },
      mounted() {
        this.displayDiff();
      },
      methods: {
        displayDiff: function() {
          this.$refs.auditContainer.appendChild(table);
          
        }
      }
    });
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <div id="app">
      <div class="diff-container" ref="auditContainer"></div>
    </div>

    【讨论】:

    • 优秀的答案!这也增加了我对 VueJS 的工作知识。谢谢!
    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2017-04-23
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多