当前 vue2

安装

yarn add monaco-editor
yarn add monaco-editor-webpack-plugin --dev

注意 vue/cli 4 要安装 “monaco-editor-webpack-plugin”: “6.0.0”, “monaco-editor”: “0.30.1”

配置vue.config.js

configureWebpack{
//.......
 plugins: [
      new MonacoWebpackPlugin({
        // available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
        languages: ['javascript', 'css', 'html', 'typescript', 'json']
      })
    ]
}

组件

<template>
  <div>
    <div id="container"></div>
  </div>
</template>
<script>
import * as monaco from 'monaco-editor'
export default {
  mounted() {
    const editor = monaco.editor.createDiffEditor(document.getElementById('container'))
    editor.setModel({
      original: monaco.editor.createModel('a'),
      modified: monaco.editor.createModel('b'),
    })
  },
}
</script>
<style lang="scss" scoped>
#container {
  height: 200px;
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #cccc;
}

.actions {
  height: 2em;
  display: flex;
  align-items: center;
  border-top: 1px solid #aaa;
  padding: 0.2em;
  box-sizing: border-box;
}

label {
  padding-right: 0.3em;
}
</style>

效果图

在这里插入图片描述

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐