对于那些尝试 jsdifflib 的人,知道此插件返回HTMLTableElement。现在我想尝试在我的VueJS组件上渲染/显示它。
我尝试了以下内容:
TEMPLATE
<div class="diff-container" v-html="dynamicHtmlContent" ref="auditContainer"></div>
COMPONENT
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组件?我也发现这很难转换为普通的字符串。请帮帮我。
答案 0 :(得分:2)
你仍然可以使用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
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;
}
}
});
&#13;
<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>
&#13;
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);
}
}
});
&#13;
<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>
&#13;