我想使用 google diff/match/patch lib
在一个 Angular 应用程序中显示两个文本之间的差异。
我的用法是这样的:
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
中导入 Angular 项目的 npm 包.
"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);
}
}
Here is a demo code
关于javascript - 在 Angular 中使用 google-diff-match-patch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63572500/