欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue-Quill-Editor富文本编辑器的使用教程

程序员文章站 2023-11-15 13:43:10
本文为大家分享了vue quill editor富文本编辑器的具体使用方法,供大家参考,具体内容如下 先看效果图:     ...

本文为大家分享了vue quill editor富文本编辑器的具体使用方法,供大家参考,具体内容如下

先看效果图:

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、存储及将数据库中的数据反显为html字符串

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

//把实体格式字符串转义成html格式的字符串
escapestringhtml(str) {
  str = str.replace(/</g,'<');
  str = str.replace(/>/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(/</g,'<');
      str = str.replace(/>/g,'>');
      return str;
    }
  },
  computed: {
    editor() {
      return this.$refs.myquilleditor.quill;
    },
  },
  mounted() {
    let content = ''; // 请求后台返回的内容字符串
    this.str = this.escapestringhtml(content);
  }
}
</script>

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。