这个富文本需要装一下插件

    "quill": "^1.3.6"
"quill-image-drop-module": "^1.0.3", //压缩图片
"quill-image-resize-module": "^3.0.0", //图片大小控制
"vue-quill-editor": "^3.0.6",

使用

webpack中加一下配置

  plugins: [
new webpack.ProvidePlugin({
'window.Quill': 'quill/dist/quill.js',
'Quill': 'quill/dist/quill.js'
})
],

main.js注册组件

// 编辑器
import VueQuillEditor from 'vue-quill-editor' import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css' Vue.use(VueQuillEditor)

页面使用

<template>
<quill-editor
v-model="content"
:content="content"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)">
</quill-editor>
</template> <script>
import { Quill } from 'vue-quill-editor'
import { ImageDrop } from 'quill-image-drop-module'
import ImageResize from 'quill-image-resize-module'
Quill.register('modules/imageDrop', ImageDrop)
Quill.register('modules/imageResize', ImageResize) export default {
data () {
return {
name: 'register-modules-example',
content: `1231`,
editorOption: {
modules: {
history: {
delay: 1000,
maxStack: 50,
userOnly: false
},
toolbar: [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
[{ 'header': 1 }, { 'header': 2 }],
[{ 'list': 'ordered' }, { 'list': 'bullet' }],
[{ 'script': 'sub' }, { 'script': 'super' }],
[{ 'indent': '-1' }, { 'indent': '+1' }],
[{ 'direction': 'rtl' }],
[{ 'size': ['small', false, 'large', 'huge'] }],
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
[{ 'font': [] }],
[{ 'color': [] }, { 'background': [] }],
[{ 'align': [] }],
['clean'],
['link', 'image', 'video']
],
imageDrop: true,
imageResize: {
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white'
},
modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]
}
}
}
}
},
methods: {
onEditorBlur (editor) {
// console.log('editor blur!', editor)
console.log(editor)
},
onEditorFocus (editor) {
// console.log('editor focus!', editor)
},
onEditorReady (editor) {
// console.log('editor ready!', editor)
}
}
}
</script>

自定义图片上传  参考处

          toolbar: {
container: toolbarOptions,
handlers: {
'image': function (value) {
if (value) {
// 调用element UI图片上传
document.querySelector('#uploadImg .el-upload').click()
} else {
this.quill.format('image', false)
}
}
}
},

handers处重写 图片的点击事件

触发 element 的 upload的点击事件

upload上传成功的回调事件

    // 图片上传成功方法
handleSuccess (response, file, fileList) {
// 获取富文本组件实例
let quill = this.$refs.QuillEditor.quill
// 如果上传成功
if (response) {
// 获取光标所在位置
let length = quill.getSelection().index
// 插入图片,第三个参数为服务器返回的图片链接地址
quill.insertEmbed(length, 'image', response.data.url)
// 调整光标到最后
quill.setSelection(length + 1)
} else {
// 提示信息,需引入Message
alert('图片插入失败')
}
}

代码

<template>
<div class="quill-editor">
<el-upload
class="upload-demo"
action="http://mock/2/api/richUpload"
accept=".jpg,.jpeg,.png,.gif]"
:headers="headers"
:show-file-list="false"
:on-success="handleSuccess"
:before-upload="beforeUpload"
id="uploadImg"
ref="uploadImg"
>
上传
</el-upload>
<quill-editor
v-model="content"
:content="content"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)"
ref="QuillEditor">
</quill-editor>
</div>
</template> <script>
import { Quill } from 'vue-quill-editor'
import { ImageDrop } from 'quill-image-drop-module'
import ImageResize from 'quill-image-resize-module'
Quill.register('modules/imageDrop', ImageDrop)
Quill.register('modules/imageResize', ImageResize) const toolbarOptions = [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
// [{ 'header': 1 }, { 'header': 2 }],
[{ 'list': 'ordered' }, { 'list': 'bullet' }],
[{ 'script': 'sub' }, { 'script': 'super' }],
// [{ 'indent': '-1' }, { 'indent': '+1' }],
// [{ 'direction': 'rtl' }],
[{ 'size': ['small', false, 'large', 'huge'] }],
// [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
[{ 'font': [] }],
[{ 'color': [] }, { 'background': [] }],
[{ 'align': [] }],
// ['clean'],
['link', 'image', 'video']
]
export default {
data () {
return {
name: 'register-modules-example',
content: `1231`,
editorOption: {
modules: {
history: {
delay: 1000,
maxStack: 50,
userOnly: false
},
toolbar: {
container: toolbarOptions,
handlers: {
'image': function (value) {
if (value) {
// 调用iview图片上传
console.log(document.querySelector('#uploadImg .el-upload'))
document.querySelector('#uploadImg .el-upload').click()
console.log(value, 1231)
} else {
console.log(212222222222222)
this.quill.format('image', false)
}
}
}
},
imageDrop: true,
imageResize: {
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white'
},
modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]
}
}
},
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('token')
}
}
},
methods: {
onEditorBlur (editor) {
// console.log('editor blur!', editor)
console.log(editor)
},
onEditorFocus (editor) {
// console.log('editor focus!', editor)
},
onEditorReady (editor) {
// console.log('editor ready!', editor)
},
// 上传之前加认证信息
beforeUpload () {
this.headers.Authorization = 'Bearer ' + sessionStorage.getItem('token')
},
// 图片上传成功方法
handleSuccess (response, file, fileList) {
// 获取富文本组件实例
let quill = this.$refs.QuillEditor.quill
// 如果上传成功
if (response) {
// 获取光标所在位置
let length = quill.getSelection().index
// 插入图片,res为服务器返回的图片链接地址
quill.insertEmbed(length, 'image', response.data.url)
// 调整光标到最后
quill.setSelection(length + 1)
} else {
// 提示信息,需引入Message
// Message.error('图片插入失败')
alert('图片插入失败')
}
}
}
}
</script>

tip: 在显示的时候会出现样式丢失的问题,需要加一个 class = "ql-editor"

<div class=" ql-editor" v-html="item.content"></div>

最新文章

  1. 【jQuery基础学习】09 jQuery与前端(这章很水)
  2. C语言关键字详解
  3. iOS 触摸的位置放一个大头针
  4. WebApp之 apple-touch-icon
  5. Java小例子——穷举质数,求平方和,求质因子。
  6. Java集群之session共享解决方案
  7. 用crontab设置svn的定期更新任务
  8. debian安装jdk6
  9. case a.ass_term_unit when &#39;01&#39; then (case a.ass_profit_mode when &#39;0&#39; then round(sum(a.ass_amount*a.ass_annual_rate/365*365*a.ass_term/100) ,2) when &#39;1&#39; then round(sum(a.ass_amount*a.ass_annual_rate/
  10. /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9: undefined reference toTIFFIsTiled@LIBTIFF_4.0&#39;
  11. VirtualBox 安装 Ubuntu 14.04 无法调节分辨率问题
  12. [论文阅读]Going deeper with convolutions(GoogLeNet)
  13. js 百度地图定位
  14. 【安富莱专题教程第6期】SEGGER的J-Scope波形上位机软件,RTT模式波形上传速度可狂飙到500KB/S左右
  15. javascript基础修炼(9)——MVVM中双向数据绑定的基本原理
  16. ELK安装(ubuntu)
  17. two week summary
  18. 用 IIS 搭建 mercurial server
  19. ArcEngine真正释放锁文件,彻底移除图层
  20. [k8s]如何处理dockerfile无expose情况下在k8s里暴漏访问

热门文章

  1. Linux基础-文件管理
  2. Java DOM解析器 - 查询XML文档
  3. Qt4 QWebView的使用例子
  4. nginx+tomcat负载均衡实验
  5. [轉]Linux 2.6内核笔记【内存管理】
  6. HDU 4652 Dice (概率DP)
  7. nginx匹配以XXX结尾的
  8. python tkinter 实现 带界面(GUI)的RSA加密、签名
  9. Oracle使用存储过程返回查询游标
  10. css3新增(圆角边框(border-radius),盒子阴影(box-shadow),文字阴影(text-shadow),背景缩放(background-size))