Vue在线代码编辑器插件 vue-monaco-editor

vue-monaco-editor

Monaco Editor Vue Component

Based off React Monaco Editor

experimental

Setup

 

npm install vue-monaco-editor --save

Simple Vue Use

 

import MonacoEditor from 'vue-monaco-editor'

 

// use in component

export default {

  components: {

    MonacoEditor

  }

}

Component Props

OptionTypeDefaultDescription
languageStringjavascript 
heightNumber/String100% 
widthNumber/String100% 
codeString// code \nInitial code to show
themeStringvs-darkvs, hc-black, or vs-dark
highlightedArray[Object][{ number: 0, class: ''}]Lines to highlight with numbers and .classes
changeThrottleNumber(ms)0throttle codeChange emit
srcPathString""see Webpack Use below
editorOptionsObjectMerged with defaults belowSee Monaco Editor Options

Editor Default Options

 

defaults: {

  selectOnLineNumbers: true,

  roundedSelection: false,

  readOnly: false,

  cursorStyle: 'line',

  automaticLayout: false,

  glyphMargin: true

}

Component Events

These events are available to parent component

EventReturnsDescription
mountededitor[editor instance]Emitted when editor has mounted
codeChangeeditor[editor instance]Emitted when code has changed

Example

Component Implementation

 

<MonacoEditor

    height="600"

    language="typescript"

    :code="code"

    :editorOptions="options"

    @mounted="onMounted"

    @codeChange="onCodeChange"

    >

</MonacoEditor>

Parent

 

module.exports = {

  components: {

    Monaco

  },

  data() {

    return {

      code: '// Type away! \n',

      options: {

        selectOnLineNumbers: false

      }

    };

  },

  methods: {

    onMounted(editor) {

      this.editor = editor;

    },

    onCodeChange(editor) {

      console.log(this.editor.getValue());

    }

  }

};

Webpack Use

By default, monaco-editor is loaded from a cdn asyncronously using require. To use a local copy of monaco-editor with webpack, we need to expose the dependency in our build directory:

npm install copy-webpack-plugin --save-dev

Add this to your webpack.config.js:

 

const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {

  plugins: [

    new CopyWebpackPlugin([

      {

        from: 'node_modules/monaco-editor/min/vs',

        to: 'vs',

      }

    ])

  ]

};

Then, specify the build directory path in the srcPath prop. See src/App.vue for an example.

Dev Use

git clone [this repo] .
npm install
npm run dev

Edit src/App.vue

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue代码中使用Monaco Editor,你可以按照以下流程进行操作: 1. 首先,你需要安装Monaco Editor和相应的webpack插件。你可以使用以下命令来安装它们: ``` npm install monaco-editor monaco-editor-webpack-plugin ``` 2. 在Vue组件的HTML代码中添加Monaco Editor的容器元素。你可以在`<template>`标签中添加类似以下的代码: ```html <template> <div id="monaco-editor-box"> <div id="monaco-editor" ref="monaco"></div> </div> </template> ``` 3. 在Vue组件的JavaScript代码中,你可以使用Monaco Editor的API来进行编辑器的初始化和配置。你可以在`<script>`标签中添加以下代码: ```javascript <script> import * as monaco from 'monaco-editor'; export default { mounted() { monaco.editor.create(this.$refs.monaco, { language: 'vue', theme: 'vs-dark', // 其他配置项 }); }, }; </script> ``` 请注意,上述代码仅提供了一个基本的示例,你可以根据需要进行额外的配置和自定义。另外,确保在Vue项目中正确引入Monaco Editor和相关的webpack插件,以便它们能够正常工作。 总结一下,要在Vue代码中使用Monaco Editor,你需要安装Monaco Editor和webpack插件,在HTML代码中添加编辑器的容器元素,在JavaScript代码中初始化和配置编辑器。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vue中使用monaco-editor](https://blog.csdn.net/weixin_45821809/article/details/129503959)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [vue2-monaco-editor](https://download.csdn.net/download/weixin_42178688/16504412)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值