javascript - 在 Angular 中使用 google-diff-match-patch
问题描述
我想google diff/match/patch lib
在一个角度应用程序中使用 来显示两个文本之间的区别。
我的用法是这样的:
public ContentHtml: SafeHtml;
compare(text1: string, text2: string):void {
var dmp = new diff_match_patch();
dmp.Diff_Timeout = 1;
dmp.Diff_EditCost = 4;
var d = dmp.diff_main(text1, text2);
dmp.diff_cleanupSemantic(d);
var ds = dmp.diff_prettyHtml(d);
this.ContentHtml = this._sanitizer.bypassSecurityTrustHtml(ds);
}
问题是,我如何导入diff_match_patch
这条线?
var dmp = new diff_match_patch();
解决方案
您需要在package.json
.
"diff-match-patch": "^1.0.5",
并在组件中导入:
import { Component } from '@angular/core';
import DiffMatchPatch from 'diff-match-patch';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular';
getDiff() {
var dmp = new DiffMatchPatch();
var diff = dmp.diff_main('Hello', 'Hallo');
// Result: [(-1, "Hell"), (1, "G"), (0, "o"), (1, "odbye"), (0, " World.")]
dmp.diff_cleanupSemantic(diff);
// Result: [(-1, "Hello"), (1, "Goodbye"), (0, " World.")]
console.log(diff);
}
}
推荐阅读
- c++ - 如何配置 basler 相机以不获取重复图像
- amazon-web-services - Lambda 定价仅限于账户或组织?
- javascript - 如何跨服务器为用户存储数据不和谐
- python - 如何使用 Twython 发送直接消息?
- db2 - 检查 DB2 中值的幂
- c# - Azure 中的多步骤任务
- amazon-cloudfront - 我们如何使用请求和标头信息监控到达云端的请求
- android - 手动创建 api 后错误删除的 Firebase 凭据 API 无法正常工作
- jenkins - 如何在不使用参数的情况下在下游构建中获取上游构建的所有者?
- python - Pythonanywhere 中的 Flask 和静态路径 url 的问题