之前使用了好几种引用tinymce的方式都已失败而告终,也找原因了。因为下面这个还是成功了,记录一下

 

1、引用tinymce-vue

npm i @tinymce/tinymce-vue -S

2、封装控件

<template>
    <div>
        <Editor :id="tinymceId" :init="init" :disabled="disabled" v-model="myValue" ></Editor>
    </div>
</template>

<script>
    //引入tinymce-vue
    import Editor from '@tinymce/tinymce-vue'
     //公共的图片前缀
    //import Global from "./Global";
    export default {
        components: {Editor},
        props: {
            //编号
            id:{
                type:String
            },
            //内容
            value: {
                type: String,
                default: ''
            },
            //是否禁用
            disabled: {
                type: Boolean,
                default: false
            },
            //插件
            plugins: {
                type: [String, Array],
                default: 'print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount imagetools textpattern help emoticons autosave bdmap indent2em autoresize formatpainter axupimgs',
            },
             //工具栏
            toolbar: {
                type: [String, Array],
                default: 'code undo redo restoredraft | fullscreen | cut copy paste pastetext | forecolor backcolor bold italic underline strikethrough link anchor | alignleft aligncenter alignright alignjustify outdent indent | \
    styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | \
    table image media charmap emoticons hr pagebreak insertdatetime print preview | bdmap indent2em lineheight formatpainter axupimgs',
            }
        },
        data() {
            let that = this;
            return {
                tinymceId: this.id || 'vue-tinymce'+ Date.parse(new Date()),
                myValue :this.value,
                init:{
                  //汉化,路径是自定义的,一般放在public或static里面,汉化文件要自己去下载
                  language_url: '/tinymce/zh_CN.js',
                  language: 'zh_CN',
                  //皮肤
                  skin: 'oxide',
                  //插件
                  plugins: this.plugins,
                  //工具栏
                  toolbar: this.toolbar,
                  //高度
                  height: 500,
                  //图片上传
                  images_upload_handler: function (blobInfo, success, failure) {
                      //文件上传的formData传递,忘记为什么要用这个了
                      let formData = new FormData();
                      formData.append('file', blobInfo.blob(), blobInfo.filename());
                      //上传的api,和后端配合,返回的是图片的地址,然后加上公共的图片前缀
                      that.$api.system.uploadImage(formData).then(res=>{
                          var url = "tt"//Global.baseUrlImg + res;
                          console.log(url)
                          success(url)
                      }).catch(res => {
                          failure('图片上传失败')
                      });
                  }
                }
            }
        },
        methods:{

        },
        watch: {
            //监听内容变化
            value(newValue) {
                this.myValue = newValue
            },
            myValue(newValue) {
                this.$emit('input', newValue)
            }
        }
    }
</script>
<style>

    .tox-notifications-container{
        display: none;
    }
    /*在页面正常使用时不用加这个样式,在弹窗使用时,要加这个样式,因为使用弹窗,z-index层数比较低,工具栏的一些工具不能使用,要将z-index层数提高。*/
.tox.tox-silver-sink.tox-tinymce-aux{ z-index: 2100 !important; } 

</style>

 

3、引用

<template>
  <div>
    <TinymceEditor :value="content" @input="newContent"></TinymceEditor>
  </div>
</template>

<script>
import TinymceEditor from "@/components/tinymce/index.vue"
export default {
    components: {
        TinymceEditor
    },
    data() {
        return {
            content:""
        }
    },
    methods: {
    //获取富文本的内容
    newContent(val){
        this.$set(this.currInfo,"content",val);
    }
  }
}
</script>

 

4、控件tinymce-vue引用的插件会从网络上下载,如:

     https://cdn.tiny.cloud/1/no-api-key/tinymce/5.10.6-132/plugins/advlist/plugin.min.js

     如果没有外网的话,可以找到文件@tinymce\tinymce-vue\lib\es2015\main\ts\components\Editor.js

     将ScriptLoader.load(this.element.ownerDocument, scriptSrc, initialise(this));

     改为红色部分
     再安装tinymce 
     npm i tinymce@5.3.1 -S
     并将node_model下的tinymce 拷贝到static文件夹下(static文件夹和src同级)
    mounted: function () {
        this.element = this.$el;
        if (getTinymce() !== null) {
            initialise(this)();
        }
        else if (this.element && this.element.ownerDocument) {
            var channel = this.$props.cloudChannel ? this.$props.cloudChannel : '5';
            var apiKey = this.$props.apiKey ? this.$props.apiKey : 'no-api-key';
            var scriptSrc = isNullOrUndefined(this.$props.tinymceScriptSrc) ?
                "https://cdn.tiny.cloud/1/" + apiKey + "/tinymce/" + channel + "/tinymce.min.js" :
                this.$props.tinymceScriptSrc;
            //ScriptLoader.load(this.element.ownerDocument, scriptSrc, initialise(this));
            ScriptLoader.load(this.element.ownerDocument, "static/tinymce/tinymce.min.js", initialise(this));
        }
    },

 

原文地址:http://www.cnblogs.com/helloStone/p/16918001.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性