TinyMCE and Vuejs as a component

久未见 提交于 2019-12-05 20:23:23
Jeff

Instead of using activeEditor you can use getInstanceById:

tinyMCE.getInstanceById(this.id).setContent(value);

Looking at the docs that might be the old version of tinyMCE, might also try:

tinymce.editors[this.id].setContent(value);

Also check out this answer, which uses a Vue directive to manage this automatically: VueJS and tinyMCE, custom directives . This allows you to make a tinyMCE editor simply with <textarea v-editor :body="body"></textarea>. You'll need to adapt it a but but directives are the way to go on this.

Another example directive: https://jsfiddle.net/edwindeveloper90/edjc82z0/

You can use vue-easy-tinymce as a component, this is a simple and powerful package for easy usage of tinymce in Vue.js project.


Or simply:

var yourComponent = {

    props: {
        id: {type: String, default: 'editor'},
        value: {default: ''}
    },

    data: function () {
        return {
            objTinymce: null
        }
    },

    template: '<textarea :id="id" :value="value"></textarea>',

    mounted: function () {

        var component = this;
        var initialOptions = {
            target: this.$el,
            init_instance_callback: function (editor) {
                editor.on('Change KeyUp Undo Redo', function (e) {
                    component.value = editor.getContent();
                });
                component.objTinymce = editor;
            }
        };
        tinymce.init(initialOptions);
    },

    watch: {
        value: function (newValue, oldValue) {
            var component = this;
            if (component.value !== this.objTinymce.getContent())
                this.objTinymce.setContent(component.value);
            else
                component.$emit('input', newValue);
        }
    }

};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!