Vue轻量级富文本编辑器-Vue-Quill-Editor

Vue轻量级富文本编辑器-Vue-Quill-Editor先看效果图:女神镇楼下载Vue-Quill-Editor npminstallvue-quill-editor–save 下载quill(Vue-Quill-Editor需要依赖) npminstallquill–save 代码 <template>…

大家好,又见面了,我是你们的朋友全栈君。

     

  先看效果图:女神镇楼
      Vue轻量级富文本编辑器-Vue-Quill-Editor

           

  1. 下载Vue-Quill-Editor
     
    
    npm install vue-quill-editor --save

     

  2. 下载quill(Vue-Quill-Editor需要依赖)
     
    npm install quill --save

     

  3. 代码
     
    <template>
        <div class="edit_container">
            <quill-editor 
                v-model="content" 
                ref="myQuillEditor" 
                :options="editorOption" 
                @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
                @change="onEditorChange($event)">
            </quill-editor>
        </div>
    </template>
    <script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    export default {
        components: {
            quillEditor
        },
        data() {
            return {
                content: `<p></p><p><br></p><ol><li><strong><em>Or drag/paste an image here.</em></strong></li><li><strong><em>rerew</em></strong></li><li><strong><em>rtrete</em></strong></li><li><strong><em>tytrytr</em></strong></li><li><strong><em>uytu</em></strong></li></ol>`,
                editorOption: {}
            }
        },
        methods: {
            onEditorReady(editor) { // 准备编辑器
    
            },
            onEditorBlur(){}, // 失去焦点事件
            onEditorFocus(){}, // 获得焦点事件
            onEditorChange(){}, // 内容改变事件
        },
        computed: {
            editor() {
                return this.$refs.myQuillEditor.quill;
            },
        }
    }
    </script>
    

    OK,搞定,简洁的富文本编辑器就展现在你眼前了,另外附上API。Vue-Quill-Editor

  4. 自定义 toolbar 菜单
     

    editorOption: {
                  placeholder: "请在这里输入",
                  modules:{
                    toolbar:[
                              ['bold', 'italic', 'underline', 'strike'],    //加粗,斜体,下划线,删除线
                              ['blockquote', 'code-block'],     //引用,代码块
                              [{ 'header': 1 }, { 'header': 2 }],        // 标题,键值对的形式;1、2表示字体大小
                              [{ 'list': 'ordered'}, { 'list': 'bullet' }],     //列表
                              [{ 'script': 'sub'}, { 'script': 'super' }],   // 上下标
                              [{ 'indent': '-1'}, { 'indent': '+1' }],     // 缩进
                              [{ 'direction': 'rtl' }],             // 文本方向
                              [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
                              [{ 'header': [1, 2, 3, 4, 5, 6, false] }],     //几级标题
                              [{ 'color': [] }, { 'background': [] }],     // 字体颜色,字体背景颜色
                              [{ 'font': [] }],     //字体
                              [{ 'align': [] }],    //对齐方式
                              ['clean'],    //清除字体样式
                              ['image','video']    //上传图片、上传视频
                              ]
                          }
                    },

     

  5. 存储及将数据库中的数据反显为HTML字符串

    后台接收到数据后会将字符中的标签进行转码,所以我们要先进行一个解码的操作让他变成标签形式的字符串:
    例如后台接收的数据如下:”&lt;h1&gt;title&lt;”  ,对应解码后就是`<h1>title</h1>`。

    //把实体格式字符串转义成HTML格式的字符串
    escapeStringHTML(str) {
        str = str.replace(/&lt;/g,'<');
        str = str.replace(/&gt;/g,'>');
        return str;
    }

    然后将返回值赋值给对应的参数:
     

    <div v-html="str" class="ql-editor">
        {
        
        {str}}
    </div>

    上面的str就是转码函数返回的值,我们要先在data中定义,所以我现在将新增跟展示放在一起,代码如下:

     

    <template>
        <div class="edit_container">
            <!--  新增时输入 -->
            <quill-editor 
                v-model="content" 
                ref="myQuillEditor" 
                :options="editorOption" 
                @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
                @change="onEditorChange($event)">
            </quill-editor>
            <!-- 从数据库读取展示 -->
            <div v-html="str" class="ql-editor">
                {
        
        {str}}
            </div>
        </div>
    </template>
    <script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    export default {
        components: {
            quillEditor
        },
        data() {
            return {
                content: `<p></p><p><br></p><ol><li><strong><em>Or drag/paste an image here.</em></strong></li><li><strong><em>rerew</em></strong></li><li><strong><em>rtrete</em></strong></li><li><strong><em>tytrytr</em></strong></li><li><strong><em>uytu</em></strong></li></ol>`,
                str: '',
                editorOption: {}
            }
        },
        methods: {
            onEditorReady(editor) { // 准备编辑器
    
            },
            onEditorBlur(){}, // 失去焦点事件
            onEditorFocus(){}, // 获得焦点事件
            onEditorChange(){}, // 内容改变事件
            // 转码
            escapeStringHTML(str) {
                str = str.replace(/&lt;/g,'<');
                str = str.replace(/&gt;/g,'>');
                return str;
            }
        },
        computed: {
            editor() {
                return this.$refs.myQuillEditor.quill;
            },
        },
        mounted() {
            let content = '';  // 请求后台返回的内容字符串
            this.str = this.escapeStringHTML(content);
        }
    }
    </script>
    

    最后提醒大家一句,插件只兼容IE10以上,不能向下兼容,如果要向下兼容,只能放弃使用这个插件。     

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134605.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • PeakVue 振动分析技术「建议收藏」

    PeakVue 振动分析技术「建议收藏」首先声明,对于振动分析,本人是菜鸟。所以本文中可能有错误,或者不够专业和严谨。最简单的振动检测指标是所谓通频值(Overallvibrationvalue)。它是采集信号的均方根:实例:产生一个正弦波,并计算rms。该值越大,反映振动越大。importnumpyasnpfromscipyimportsignalimportmatplotlib.pyplot…

    2022年10月16日
  • macbook下载的安装包怎么安装_mac安装软件拉进application

    macbook下载的安装包怎么安装_mac安装软件拉进applicationMac下面除了用dmg、pkg来安装软件外,比较方便的还有用MacPorts来帮助你安装其他应用程序,跟BSD中的ports道理一样。MacPorts就像apt-get、yum一样,可以快速安装些软件。下面将MacPorts的安装和使用方法记录在这里以备查。访问官方网站http://www.macports.org/install.php,这里提供有dmg安装和源码安装两种方式,d

    2022年9月21日
  • pycharm虚拟环境的解释器设置_pycharm虚拟环境

    pycharm虚拟环境的解释器设置_pycharm虚拟环境什么是Pycharm中的虚拟环境 假如想要在Pycharm中建立两个项目,并且这两个项目需要用到同一个第三方库的不同版本,如果这两个项目共享一个运行环境,那么此时就会发生版本冲突问题。为了解决这个问题,Pycharm提供了Virtualenv(即,虚拟环境)。Virtualenv可以创建一套独立运行的Python环境,从而做到不同项目之间的隔离。当需要安装该环境所需要的包时,在设置—项目—…

    2022年10月29日
  • pytest运行_ios12自动清理缓存

    pytest运行_ios12自动清理缓存前言pytest运行完用例之后会生成一个.pytest_cache的缓存文件夹,用于记录用例的ids和上一次失败的用例。方便我们在运行用例的时候加上–lf和–ff参数,快速运行上一

    2022年7月28日
  • @RequestBody 和 @RequestParam可以同时使用

    @RequestBody 和 @RequestParam可以同时使用@RequestParam和@RequestBody这两个注解是可以同时使用的。网上有很多博客说@Requestparam和@RequestBody不能同时使用,这是错误的。根据HTTP协议,并没有说post请求不能带URL参数,经验证往一个带有参数的URL发送post请求也是可以成功的。自己个人实际验证结果:packagecom.example.model;import…

    2022年6月16日
  • tdscdma手机linux,全方位介绍——TD-SCDMA无线技术(1)[通俗易懂]

    tdscdma手机linux,全方位介绍——TD-SCDMA无线技术(1)[通俗易懂]全方位介绍——TD-SCDMA无线技术(1)无线3G中的TD-SCDMA无线技术绝对是中国人所关注的重点,不仅仅因为它是我国自主研发的,更因为它是结合了多种高端无线通信技术的联合体。那么,本文将为大家详细介绍TD-SCDMA无线技术的知识。一,TD-SCDMA无线技术是什么?TD是TD-SCDMA——TimeDivision-SynchronousCodeDivisionMultiple…

    2022年10月4日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号