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

      

  • 相关阅读:
    wss的webpart的3种开发方式(转载)
    C# 2.0学习之集合2
    对C#中的TreeView添加背景图转载
    ASP.NET 2.0: 页面中链入的CSS、js文件带中文时需注意
    C# 2.0学习之泛型
    C# 2.0学习之数组
    连接 ACCESS 2007
    C# 2.0学习之事件2
    一个P2P+搜索音乐网站的策划书(转载)
    关于MOSS的应用和开发的一些联接
  • 原文地址:https://www.cnblogs.com/shuihanxiao/p/10906575.html
Copyright © 2011-2022 走看看