網站首頁 編程語言 正文
mocano editor中使用代碼比對功能
環境:vue2.x + typescript
使用前提請先看:vue2.x中monaco-editor的基礎使用
效果圖:
創建代碼公共組件
codeDiff.vue
<template>
<div class="editor" :id="id"></div>
</template>
<script lang="ts">
import CodeDiff from "./codeDiff";
export default CodeDiff;
</script>
<style lang="less" scoped>
.editor {
width: 100%;
height: 635px;
text-align: left;
}
</style>
codeDiff.ts
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
import * as Monaco from "monaco-editor";
interface ICode {
value: string;
language: string;
}
@Component
export default class CodeDiff extends Vue {
diffInstance: any;
id = "";
@Prop({ default: { value: "", language: "" } })
oldCode!: ICode;
@Prop({ default: { value: "", language: "" } })
code!: ICode;
created(): void {
this.id = `editor${Math.random()}`;
}
@Watch("oldCode", { deep: true })
oldWatch(): void {
this.change();
}
@Watch("code", { deep: true })
nowWatch(): void {
this.change();
}
mounted(): void {
this.init();
}
setModel(instance: any): void {
// 此處可以把`original`和`modified`按情況創建模型,比如初始兩個都渲染,`original`
改變只創建`original`的model,`modified`改變只創建`modified`的model
instance.setModel({
original: Monaco.editor.createModel(
this.oldCode.value,
this.oldCode.language,
),
modified: Monaco.editor.createModel(
this.code.value,
this.code.language,
),
})
}
init(): void {
const code: HTMLElement = document.getElementById(this.id) as HTMLElement;
this.diffInstance = Monaco.editor.createDiffEditor(code, {
wordWrap: "on",
scrollBeyondLastLine: false,
automaticLayout: true,
readOnly: true,
});
this.setModel(this.diffInstance);
}
change(): void {
if (this.diffInstance === null) {
this.init();
return;
}
this.setModel(this.diffInstance);
}
}
使用組件
<code-diff :old-code="oldCodeInfo" :code="codeInfo" />
原文鏈接:https://blog.csdn.net/qq_38157825/article/details/124378219
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-04-27 VSCode?IDE?配置環境過程解析_C 語言
- 2022-04-28 shell?腳本中獲取命令的輸出的實現示例_linux shell
- 2022-11-21 正則表達式RegExp語法與用法詳解_正則表達式
- 2022-07-23 C語言簡明分析指針與引用的具體用法_C 語言
- 2022-11-08 C語言實現時間處理工具的示例代碼_C 語言
- 2022-07-21 CentOS 網絡設置修改
- 2022-08-25 C/C++內存管理基礎與面試_C 語言
- 2022-06-25 Python制作簡易計算器功能_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支