对于那些尝试过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 组件?我也发现这很难转换成纯字符串。请帮助我。