zoukankan      html  css  js  c++  java
  • Vue-Quill-Editor 富文本编辑器的使用

    步骤如下:

    1、下载Vue-Quill-Editor

            npm install vue-quill-editor --save

    2、下载quill(Vue-Quill-Editor需要依赖)

      npm install quill --save

    3、引入对应的css和js

      import { quillEditor } from "vue-quill-editor"; //调用编辑器
      import 'quill/dist/quill.core.css';
      import 'quill/dist/quill.snow.css';
      import 'quill/dist/quill.bubble.css';

    4、代码如下

      

    <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>

    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以上,不能向下兼容,如果要向下兼容,只能放弃使用这个插件。

      

  • 相关阅读:
    一张图,理解JAVA体系结构、运行机制、JVN运行机制、Java平台(初学)
    高效的CSS代码(2)
    高效的CSS代码(1)
    hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法
    从tomcat下载文件的配置方法(很全呢)
    mysql中对于时间的处理,时间的滚动,求时间间隔,切换时区等等
    分享一个在js中判断数据是undefined,NaN,null,的技巧
    Java中Date类型如何向前向后滚动时间,( 附工具类)
    如何让tomcat服务器运行在80端口,并且无需输入项目名即可访问项目()
    前端的字符串时间如何自动转换为后端Java的Date属性,介绍springMVC中如何解决时间转换问题
  • 原文地址:https://www.cnblogs.com/shuihanxiao/p/10906575.html
Copyright © 2011-2022 走看看