vue中使用Tinymce的示例代码

 更新时间:2023年08月17日 09:00:34   作者:一个叶小小  
这篇文章主要介绍了vue中使用Tinymce的示例,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1、安装tinymce编辑器

npm i tinymce
npm i @tinymce/tinymce-vue

或:

yarn add tinymce
yarn add @tinymce/tinymce-vue

2、配置中文语言包

地址:中文语言包

注:最好将语言包放在public/langs/或static/langs/目录下,下面组件将会引用

3、封装组件

在components 目录下新建 tinymce.vue

<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: 'preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media code codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount help emoticons autosave autoresize formatpainter',
        },
        //工具栏
        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: {
                //汉化,路径是自定义的
                language_url: '/tinymce/langs/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>

4、引用组件

新建test.vue

<template>
    <div>
        <TinymceEditor :value="content" @input="newContent"></TinymceEditor>
    </div>
</template>
<script>
import TinymceEditor from "./components/tinymce.vue"
export default {
    components: {
        TinymceEditor
    },
    data() {
        return {
            content: ""
        }
    },
    methods: {
        // 获取富文本的内容
        newContent(val) {
            this.content = val; // 直接更新 content 属性
        }
    }
}
</script>

注:文件引入路径是关键

到此这篇关于vue中使用Tinymce的文章就介绍到这了,更多相关vue使用Tinymce内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 深入Vue-Router路由嵌套理解

    深入Vue-Router路由嵌套理解

    这篇文章主要介绍了深入Vue-Router路由嵌套理解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Vue程序调试和排错技巧分享

    Vue程序调试和排错技巧分享

    因为程序的调试非常重要,程序猿可以利用好的调试方法去查找定位自己的问题所在之处,从而,达到纠正自己程序错误的地方,健壮自己的程序,让问题变得越来越少,程序变得越来越健康,所以本文给大家介绍了Vue程序调试和排错技巧,需要的朋友可以参考下
    2024-12-12
  • axios全局请求参数设置,请求及返回拦截器的方法

    axios全局请求参数设置,请求及返回拦截器的方法

    下面小编就为大家分享一篇axios全局请求参数设置,请求及返回拦截器的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • vue axios基于常见业务场景的二次封装的实现

    vue axios基于常见业务场景的二次封装的实现

    这篇文章主要介绍了vue axios基于常见业务场景的二次封装的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • vue 实现网页截图功能详解

    vue 实现网页截图功能详解

    这篇文章主要介绍了通过vue实现网页截图的功能,有兴趣的童鞋可以了解一下
    2021-11-11
  • vuex中this.$store.commit和this.$store.dispatch的基本用法实例

    vuex中this.$store.commit和this.$store.dispatch的基本用法实例

    在vue的项目里常常会遇到父子组件间需要进行数据传递的情况,下面这篇文章主要给大家介绍了关于vuex中this.$store.commit和this.$store.dispatch的基本用法的相关资料,需要的朋友可以参考下
    2023-01-01
  • Vue.js构建你的第一个包并在NPM上发布的方法步骤

    Vue.js构建你的第一个包并在NPM上发布的方法步骤

    这篇文章主要介绍了Vue.js构建你的第一个包并在NPM上发布的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • 手写Vue源码之数据劫持示例详解

    手写Vue源码之数据劫持示例详解

    这篇文章主要给大家介绍了手写Vue源码之数据劫持的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • 一文带你简单理解Vue的data为何只能是函数

    一文带你简单理解Vue的data为何只能是函数

    如果data是一个函数的话,这样每复用一次组件,就会返回一份新的data,下面这篇文章主要给大家介绍了关于简单理解Vue的data为啥只能是函数的相关资料,需要的朋友可以参考下
    2022-10-10
  • vue获取验证码倒计时组件

    vue获取验证码倒计时组件

    这篇文章主要为大家详细介绍了vue获取验证码倒计时组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08

最新评论