Sign up (with export icon)

Integrating CKeditor 5 with Vue.js 2.x from npm

Contribute to this guide Show the table of contents

You can use CKEditor 5 Vue 2 component to add a rich text editor to your application. This guide will help you install and configure the npm distribution of the CKEditor 5.

Create your own CKEditor 5

Check out our interactive Builder to quickly get a taste of CKEditor 5. It offers an easy-to-use user interface to help you configure, preview, and download the editor suited to your needs.

  • editor type,
  • the features you need,
  • the preferred framework (React, Angular, Vue or Vanilla JS),
  • the preferred distribution method.

You get ready-to-use code tailored to your needs!

Check out our interactive Builder

Quick start

Copy link

This guide assumes you already have a Vue project. First, install the CKEditor 5 packages:

  • ckeditor5 – package with open-source plugins and features.
  • ckeditor5-premium-features – package with premium plugins and features.
npm install ckeditor5 ckeditor5-premium-features
Copy code

Depending on your configuration and chosen plugins, you may need to install the first or both packages.

Then, install the CKEditor 5 WYSIWYG editor component for Vue 2:

npm install @ckeditor/ckeditor5-vue2
Copy code

To create an editor instance, you must first import the editor and the component modules into the root file of your application (for example, main.js when generated by create-vue).

Note

Starting from version 44.0.0, the licenseKey property is required to use the editor. If you use a self-hosted editor from npm:

You can set up a free trial to test the editor and evaluate the self-hosting.

import Vue from 'vue';
import CKEditor from '@ckeditor/ckeditor5-vue2';
import App from './App.vue';

Vue.use( CKEditor );

new Vue( App ).$mount( '#app' );
Copy code

Use the <ckeditor> component inside the template tag. The below example shows how to use the component with open-source and premium plugins.

<template>
    <ckeditor :editor="editor" v-model="editorData" :config="editorConfig" />
</template>

<script>
import { ClassicEditor, Essentials, Paragraph, Bold, Italic } from 'ckeditor5';
import { FormatPainter } from 'ckeditor5-premium-features';

import 'ckeditor5/ckeditor5.css';
import 'ckeditor5-premium-features/ckeditor5-premium-features.css';

export default {
    name: 'app',
    data() {
        return {
            editor: ClassicEditor,
            editorData: '<p>Hello from CKEditor 5 in Vue 2!</p>',
            editorConfig: {
                licenseKey: '<YOUR_LICENSE_KEY>', // Or 'GPL'.
                plugins: [ Essentials, Paragraph, Bold, Italic, FormatPainter ],
                toolbar: [ 'undo', 'redo', '|', 'bold', 'italic', '|', 'formatPainter' ]
            }
        };
    }
};
</script>
Copy code

Using the component locally

Copy link

If you do not want the CKEditor component to be enabled globally, you can skip the Vue.use( CKEditor ) part entirely. Instead, configure it in the components property of your view.

<template>
    <ckeditor :editor="editor" v-model="editorData" :config="editorConfig" />
</template>

<script>
import CKEditor from '@ckeditor/ckeditor5-vue2';
import { Bold, ClassicEditor, Essentials, Italic, Paragraph } from 'ckeditor5';

import 'ckeditor5/ckeditor5.css';

export default {
    name: 'app',
    components: {
        ckeditor: CKEditor.component
    },
    data() {
        return {
            editor: ClassicEditor,
            editorData: '<p>Hello from CKEditor 5 in Vue 2!</p>',
            editorConfig: {
                licenseKey: '<YOUR_LICENSE_KEY>', // Or 'GPL'.
                plugins: [ Bold, Essentials, Italic, Paragraph ],
                toolbar: [ 'undo', 'redo', '|', 'bold', 'italic' ]
            }
        };
    }
};
</script>
Copy code

Component directives

Copy link

editor

Copy link

This directive specifies the editor to be used by the component. It must directly reference the editor constructor to be used in the template.

<template>
    <ckeditor :editor="editor" />
</template>

<script>
    import { ClassicEditor } from 'ckeditor5';

    export default {
        name: 'app',
        data() {
            return {
                editor: ClassicEditor,

                // ...
            };
        }
    };
</script>
Copy code

tag-name

Copy link

By default, the editor component creates a <div> container which is used as an element passed to the editor (for example, ClassicEditor#element). The element can be configured, so for example to create a <textarea>, use the following directive:

<ckeditor :editor="editor" tag-name="textarea" />
Copy code

v-model

Copy link

A standard directive for form inputs in Vue. Unlike value, it creates a two–way data binding, which:

  • Sets the initial editor content.
  • Automatically updates the state of the application as the editor content changes (for example, as the user types).
  • Can be used to set the editor content when necessary.
<template>
    <div>
        <ckeditor :editor="editor" v-model="editorData" />
        <button v-on:click="emptyEditor()">Empty the editor</button>

        <h2>Editor data</h2>
        <code>{{ editorData }}</code>
    </div>
</template>

<script>
    import { ClassicEditor } from 'ckeditor5';

    export default {
        name: 'app',
        data() {
            return {
                editor: ClassicEditor,
                editorData: '<p>Content of the editor.</p>'
            };
        },
        methods: {
            emptyEditor() {
                this.editorData = '';
            }
        }
    };
</script>
Copy code

In the above example, the editorData property will be updated automatically as the user types and changes the content. It can also be used to change (as in emptyEditor()) or set the initial content of the editor.

If you only want to execute an action when the editor data changes, use the input event.

value

Copy link

Allows a one–way data binding that sets the content of the editor. Unlike v-model, the value will not be updated when the content of the editor changes.

<template>
    <ckeditor :editor="editor" :value="editorData" />
</template>

<script>
    import { ClassicEditor } from 'ckeditor5';

    export default {
        name: 'app',
        data() {
            return {
                editor: ClassicEditor,
                editorData: '<p>Content of the editor.</p>'
            };
        }
    };
</script>
Copy code

To execute an action when the editor data changes, use the input event.

config

Copy link

Specifies the configuration of the editor.

<template>
    <ckeditor :editor="editor" :config="editorConfig" />
</template>

<script>
    import { ClassicEditor } from 'ckeditor5';

    export default {
        name: 'app',
        data() {
            return {
                editor: ClassicEditor,
                editorConfig: {
                    toolbar: [ 'bold', 'italic', '|', 'link' ]
                }
            };
        }
    };
</script>
Copy code

disabled

Copy link

This directive controls the isReadOnly property of the editor.

It sets the initial read–only state of the editor and changes it during its lifecycle.

<template>
    <ckeditor :editor="editor" :disabled="editorDisabled" />
</template>

<script>
    import { ClassicEditor } from 'ckeditor5';

    export default {
        name: 'app',
        data() {
            return {
                editor: ClassicEditor,
                // This editor will be read–only when created.
                editorDisabled: true
            };
        }
    };
</script>
Copy code

Component events

Copy link

ready

Copy link

Corresponds to the ready editor event.

<ckeditor :editor="editor" @ready="onEditorReady" />
Copy code

focus

Copy link

Corresponds to the focus editor event.

<ckeditor :editor="editor" @focus="onEditorFocus" />
Copy code

blur

Copy link

Corresponds to the blur editor event.

<ckeditor :editor="editor" @blur="onEditorBlur" />
Copy code

input

Copy link

Corresponds to the change:data editor event. See the v-model directive to learn more.

<ckeditor :editor="editor" @input="onEditorInput" />
Copy code

destroy

Copy link

Corresponds to the destroy editor event.

Note: Because the destruction of the editor is promise–driven, this event can be fired before the actual promise resolves.

<ckeditor :editor="editor" @destroy="onEditorDestroy" />
Copy code

How to?

Copy link

Using the Document editor type

Copy link

If you use the Document (decoupled) editor in your application, you need to manually add the editor toolbar to the DOM.

Since accessing the editor toolbar is not possible until after the editor instance is ready, put your toolbar insertion code in a method executed upon the ready event of the component, like in the following example:

<template>
    <ckeditor :editor="editor" @ready="onReady" />
</template>

<script>
    import { DecoupledEditor, Bold, Essentials, Italic, Paragraph } from 'ckeditor5';

    import 'ckeditor5/ckeditor5.css'

    export default {
        name: 'app',
        data() {
            return {
                editor: DecoupledEditor,
                // ...
            };
        },
        methods: {
            onReady( editor )  {
                // Insert the toolbar before the editable area.
                editor.ui.getEditableElement().parentElement.insertBefore(
                    editor.ui.view.toolbar.element,
                    editor.ui.getEditableElement()
                );
            }
        }
    };
</script>
Copy code

Localization

Copy link

CKEditor 5 supports multiple UI languages, and so does the official Vue 2 component. Follow the instructions below to translate CKEditor 5 in your Vue application.

Similarly to CSS style sheets, both packages have separate translations. Import them as shown in the example below. Then, pass them to the translations array inside the editorConfig prop in the component.

<template>
    <ckeditor :editor="editor" v-model="editorData" :config="editorConfig" />
</template>

<script>
import { ClassicEditor, Bold, Essentials, Italic, Paragraph } from 'ckeditor5';
// More imports...

import coreTranslations from 'ckeditor5/translations/es.js';
import premiumFeaturesTranslations from 'ckeditor5-premium-features/translations/es.js';

export default {
    name: 'app',
    data() {
        return {
            editor: ClassicEditor,
            editorData: '<p>Hola desde CKEditor 5 en Vue 2!</p>',
            editorConfig: {
                // ... Other configuration options ...
                translations: [ coreTranslations, premiumFeaturesTranslations ]
            }
        };
    }
};
</script>
Copy code

For more information, refer to the “Setting UI language” guide.

Contributing and reporting issues

Copy link

The source code of this component is available on GitHub in https://github.com/ckeditor/ckeditor5-vue2.

Next steps

Copy link