각도에서 google-diff-match-patch 사용
Aug 25 2020
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();
답변
1 ShashankVivek Aug 25 2020 at 13:02
.NET에서 각도 프로젝트 용 npm 패키지를 가져와야합니다 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);
}
}
다음은 데모 코드입니다.