SoFunction
Updated on 2025-03-09

Code editor and real-time preview function in Vue

When implementing code editor and real-time preview functions in Vue projects, some front-end libraries are usually used to simplify this task. This article will introduce how to use CodeMirror and implement code editor and real-time preview functions in Vue. We will first introduce the basic concepts of CodeMirror and then demonstrate how to combine them to create a practical code editor and live preview functionality.

1. What is CodeMirror?

CodeMirror is a popular open source JavaScript code editor library that provides rich editor functions such as syntax highlighting, code folding, intelligent indentation, etc. CodeMirror is easy to integrate into Vue projects and can be used to create code editor components, allowing users to easily edit code.

To use CodeMirror in a Vue project, you first need to install it. Run the following command in the project directory:

npm install codemirror

Then, CodeMirror can be introduced in the Vue component and used to create a code editor.

2. Create CodeMirror Code Editor Component

First, let's create a Vue component for wrapping CodeMirror code editor.

<template>
  <div>
    <textarea ref="editor"></textarea>
  </div>
</template>
<script>
import 'codemirror/lib/';
import 'codemirror/theme/';
import 'codemirror/mode/javascript/';
import CodeMirror from 'codemirror';
export default {
  props: {
    value: String,
  },
  mounted() {
    const editor = CodeMirror(this.$, {
      mode: 'javascript',
      theme: 'material',
      lineNumbers: true,
    });
    ();
    ('change', () => {
      this.$emit('input', ());
    });
  },
};
</script>

The above code creates a Vue component containing the CodeMirror editor, which accepts a name calledvalue prop, used to pass the initial code. The components are inmounted Initialize the CodeMirror editor in the hook and passv-model Bind the code that is updated in real time.

3. Create a real-time preview component

Now, we will create another Vue component for displaying a live preview. Let's create a name called Components:

&lt;template&gt;
  &lt;div&gt;
    &lt;iframe ref="preview" :srcdoc="compiledCode"&gt;&lt;/iframe&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {
  props: {
    code: String,
  },
  computed: {
    compiledCode() {
      // Here you can use appropriate methods to compile the code into HTML, CSS, JavaScript, etc.      // For example, if your code is JavaScript, you can use Babel to compile it      // This is only for demonstration, and it needs to be compiled according to the needs in actual projects      return `&lt;html&gt;
                &lt;head&gt;&lt;/head&gt;
                &lt;body&gt;
                  &lt;script&gt;
                    ${}
                  &lt;/script&gt;
                &lt;/body&gt;
              &lt;/html&gt;`;
    },
  },
};
&lt;/script&gt;

In this component, we use a<iframe> Elements to display live previews.code Properties are used to receive code passed from the code editor. existcomputed In the properties, we compiled the code and used it assrcdoc Bind to<iframe> superior.

4. Use code editor and live preview in parent component

Now we can use the two child components we created in the parent component, namely the code editor and the live preview component.

&lt;template&gt;
  &lt;div&gt;
    &lt;h1&gt;VueCode editor with live preview examples&lt;/h1&gt;
    &lt;div class="editor-preview-container"&gt;
      &lt;CodeEditor v-model="code"&gt;&lt;/CodeEditor&gt;
      &lt;Preview :code="code"&gt;&lt;/Preview&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
import CodeEditor from './';
import Preview from './';
export default {
  components: {
    CodeEditor,
    Preview,
  },
  data() {
    return {
      code: `('Hello, World!');`,
    };
  },
};
&lt;/script&gt;
&lt;style scoped&gt;
.editor-preview-container {
  display: flex;
  flex-direction: row;
}
&lt;/style&gt;

In the parent component, we introduceCodeEditor andPreview components and use them in templates. We initialized acode Data property, which will serve as the default code for the code editor.

5. Summary

Through the above steps, we successfully implemented the code editor and real-time preview function in the Vue project. CodeMirror provides powerful code editing functions, making component creation and data binding very simple. When users edit code, real-time previews will be updated according to the changes in the code, thus providing users with an interactive programming environment. In actual projects, you can customize the compilation and preview logic of the code as needed to meet specific needs.

This is the article about how to perform code editors and real-time previews in Vue. For more related vue code editor content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!