您的位置:首页 > 文旅 > 旅游 > 一键生成微信小程序_广告页面制作_最新网站查询_关键字优化

一键生成微信小程序_广告页面制作_最新网站查询_关键字优化

2025/7/3 14:29:35 来源:https://blog.csdn.net/Roy__Jason/article/details/147476552  浏览:    关键词:一键生成微信小程序_广告页面制作_最新网站查询_关键字优化
一键生成微信小程序_广告页面制作_最新网站查询_关键字优化

用法一:computed

<div class="card" v-if="showFlag"><div class="info">*红色背景为已删除内容,绿色背景为新增内容</div><el-form-item label="与上季度比对:"><div class="compareCss" v-html="highlightedLastQuarteDiff"></div></el-form-item>
</div>

安装插件:diff

npm install diff
import diff from 'diff';
// 与上季度比对
const highlightedLastQuarteDiff = computed(() => {const oldText = state.jsonContent ?? ''; // 旧值(原内容)const newText = state.content ?? ''; // 新值(修改后内容)const differences = diff.diffChars(oldText, newText); // 字符级差异对比let result = '';differences.forEach(part => {// 被删除的内容(旧值有,新值无)if (part.removed) {result += `<span style="text-decoration: line-through; background:#fbe9eb;color: red;  text-decoration-color: #777;">${part.value}</span>`;}// 新增的内容(旧值无,新值有)else if (part.added) {result += `<span style="background-color: #a6f3a6; color: #777;">${part.value}</span>`;}// 未改动的内容else {result += part.value;}});return result;
});

用法二:封装成方法

<div class="compareTableCss"><div v-for="cItem in compareTable" :key="cItem.line"><p>{{ cItem.title }}</p><p v-html="cItem.content"></p></div>
</div>
function highlightTextDifferences(oldText: string | null | undefined, newText: string | null | undefined): string {// 处理 null/undefined 情况const safeOldText = oldText ?? '';const safeNewText = newText ?? '';try {const differences = diff.diffChars(safeOldText, safeNewText);let result = '';differences.forEach((part:any) => {if (part.removed) {result += `<span style="text-decoration: line-through; background:#fbe9eb;color: red; text-decoration-color: #777;">${escapeHtml(part.value)}</span>`;} else if (part.added) {result += `<span style="background-color: #a6f3a6; color: #777;">${escapeHtml(part.value)}</span>`;} else {result += escapeHtml(part.value);}});return result;} catch (error) {console.error('Error in text comparison:', error);return escapeHtml(safeNewText); // 出错时返回新文本}
}function escapeHtml(text: string): string {const div = document.createElement('div');div.textContent = text;return div.innerHTML;
}// 使用
state.compareTable[1].content = highlightTextDifferences(fItem.windMuBiao, fItem.muBiao);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com