Vue-Quill-Editor富文本编辑器的使用教程
程序员文章站
2023-11-15 13:43:10
本文为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 微信支付宝入局移动电影票务
下一篇: 如何禁止他人添加打印机
推荐阅读
-
Vue-Quill-Editor富文本编辑器的使用教程
-
vue集成百度UEditor富文本编辑器使用教程
-
vue-quill-editor富文本编辑器简单使用方法
-
详解Vue基于vue-quill-editor富文本编辑器使用心得
-
微信小程序整合使用富文本编辑器的方法详解
-
React中使用UEditor百度富文本的方法
-
SpringBoot使用Editor.md构建Markdown富文本编辑器示例
-
在 Vue 项目中引入 tinymce 富文本编辑器的完整代码
-
在Asp.Net或.Net Core中配置使用MarkDown富文本编辑器有开源模板代码(代码是.net core3.0版本)
-
在Asp.Net Core中配置使用MarkDown富文本编辑器实现图片上传和截图上传(开源代码.net core3.0)