Monaco Editor Component for Vue

1.0.10 · active · verified Sun Apr 19

monaco-editor-vue is a Vue.js component that integrates the powerful Monaco Editor, the code editor that powers VS Code. As of version 1.0.10, it provides a convenient wrapper for embedding the editor within Vue applications, simplifying initialization and event handling. While the package itself is relatively small, it relies heavily on the `monaco-editor` core, which is a significant dependency. Key differentiators include its declarative API for setting properties like `language`, `theme`, and `options`, and handling editor events through standard Vue patterns. It's actively maintained with a stable API, though specific release cadence isn't explicitly defined, it follows standard npm versioning. A crucial aspect of its usage is the requirement for `monaco-editor-webpack-plugin` for proper bundle optimization and resource loading.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `monaco-editor-vue` in a Vue 3 application with TypeScript, including the necessary Webpack configuration for the `monaco-editor-webpack-plugin`. It showcases basic usage with properties like `width`, `height`, `theme`, `language`, `options`, `value` (for controlled mode), and event handling for `change`, `editorBeforeMount`, and `editorMounted`.

<!-- vue.config.js for Vue CLI (or webpack.config.js for plain webpack) -->
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = {
  chainWebpack: config => {
    config.plugin('monaco-editor').use(MonacoWebpackPlugin, [
      {
        languages: ['json', 'javascript', 'html', 'typescript', 'css']
      }
    ]);
  },
  // For older Vue CLI or plain webpack, ensure your publicPath is configured correctly if deploying to a sub-path
  // publicPath: process.env.NODE_ENV === 'production' ? '/my-app/' : '/' 
};

// src/App.vue
<template>
  <div id="app">
    <h1>Code Editor</h1>
    <MonacoEditor
      width="800"
      height="500"
      theme="vs-dark"
      language="javascript"
      :options="editorOptions"
      :value="code"
      @change="onChange"
      @editorBeforeMount="handleEditorBeforeMount"
      @editorMounted="handleEditorMounted"
    ></MonacoEditor>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import MonacoEditor from 'monaco-editor-vue';

export default defineComponent({
  name: 'App',
  components: {
    MonacoEditor
  },
  setup() {
    const code = ref('function hello() {\n  console.log("Hello Monaco Editor!");\n}');
    const editorOptions = ref({
      selectOnLineNumbers: true,
      minimap: { enabled: true },
      tabSize: 2
    });

    const onChange = (value: string) => {
      console.log('Editor value changed:', value);
      code.value = value; // Update model for controlled mode
    };

    const handleEditorBeforeMount = (monaco: any) => {
      // Optionally register custom themes or languages here
      console.log('Monaco editor before mount:', monaco);
    };

    const handleEditorMounted = (editor: any, monaco: any) => {
      console.log('Monaco editor mounted:', editor, monaco);
      // You can access the editor instance here to call its methods
    };

    return {
      code,
      editorOptions,
      onChange,
      handleEditorBeforeMount,
      handleEditorMounted
    };
  }
});
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

view raw JSON →