版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/senmage/article/details/82388728
先看效果图:女神镇楼
- 下载Vue-Quill-Editor
npm install vue-quill-editor --save
- 下载quill(Vue-Quill-Editor需要依赖)
npm install quill --save
- 代码
<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>`, } }, methods: { onEditorReady(editor) { // 准备编辑器 } }, computed: { editor() { return this.$refs.myQuillEditor.quill; }, } } </script>
OK,搞定,简洁的富文本编辑器就展现在你眼前了,另外附上API。Vue-Quill-Editor
-
存储及将数据库中的数据反显为HTML字符串
后台接收到数据后会将字符中的标签进行转码,所以我们要先进行一个转码的操作让他变成标签形式的字符串:
//把实体格式字符串转义成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: '' } }, methods: { onEditorReady(editor) { // 准备编辑器 }, // 转码 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>