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

    "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>

vue富文本vue-quill-editor的更多相关文章

  1. vue 富文本编辑器 项目实战用法

    1.挑个富文本编辑器 首先针对自己项目的类型,确定自己要用啥编辑器. 1.1 wangeditor 如果一般类似博客这种项目不需要花里胡哨的,功能也不要求贼多的,推荐一下wangeditor(点击跳转 ...

  2. vue+富文本编辑器UEditor

    vue+富文本编辑器UEditor 昨天的需求是把textarea换成富文本编辑器的形式, 网上找了几种富文本编辑器ueditor.tinymce等, 觉得ueditor实现双向绑定还挺有意思, 分享 ...

  3. 富文本编辑器...quill 的使用放...

    移动端 quill 时候用的 是 div 而不是 textarea.... 引入 dom <link href="//cdn.quilljs.com/1.3.6/quill.snow. ...

  4. 现代富文本编辑器Quill的内容渲染机制

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

  5. vue富文本编辑器vue-quill-editor使用总结(包含图片上传,拖拽,放大和缩小)

    vue-quill-editor是vue很好的富文本编辑器,富文本的功能基本上都支持,样式是黑白色,简洁大方. 第一步下载 vue-quill-editor: npm i vue-quill-edit ...

  6. Vue富文本编辑器(图片拖拽缩放)

    富文本编辑器(图片拖拽缩放) 需求: 根据业务要求,需要能够上传图片,且上传的图片能在移动端中占满屏幕宽度,故需要能等比缩放上传的图片,还需要能拖拽.缩放.改变图片大小.尝试多个第三方富文本编辑器,很 ...

  7. 富文本编辑器Quill(一)简单介绍

    Quill是一个很流行的富文本编辑器,github上star大约21k: github:https://github.com/quilljs/quill/ 官网: https://quilljs.co ...

  8. 富文本编辑器Quill(二)上传图片与视频

    image与video在Quill formats中属于Embeds,要在富文本中插入图片或者视频需要使用insertEmbed api. insertEmbed insertEmbed(index: ...

  9. 现代富文本编辑器Quill的模块化机制

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

随机推荐

  1. JS 判断是否为null

    1.判断undefined: var tmp = undefined; if (typeof(tmp) == "undefined"){ alert("undefined ...

  2. css代码思考:display和float

    关于display <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. VC的小工具查询exe的依赖

    查看程序或动态库所依赖的动态库 dumpbin /dependents  abc.exe 查看动态库的输出函数 dumpbin /exports abc.dll

  4. shell eval命令

    1. eval command-line 其中command-line是在终端上键入的一条普通命令行.然而当在它前面放上eval时,其结果是shell在执行命令行之前扫描它两次.如: pipe=&qu ...

  5. css篇-简化版

    [CSS篇]简化版 (1)     CSS盒模型 CSS盒模型 题目:谈谈你对CSS盒模型的认识 1)       基本概念:标准模型+IE模型 2)       标准模型和IE模型的区别 计算宽度和 ...

  6. python-模块 time, os, sys

    时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time( ...

  7. HashSet源码解析笔记

    HashSet是基于HashMap实现的.HashSet底层采用HashMap来保存元素,因此HashSet底层其实比较简单. HashSet是Set接口典型实现,它按照Hash算法来存储集合中的元素 ...

  8. python之arrow时间处理模块

    首先安装 pip install arrow 直接创建arrow对象 print(arrow.get(2019, 1, 23)) # 2019-01-23T00:00:00+00:00 print(a ...

  9. dns轮训python

    环境 python3 先安装dnspython模块 httpclient模块 resolver模块 pip install dnspython pip install hhtpclient pip i ...

  10. JS window对象 返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL。

    返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL. 如果倒退之后,再想回到倒退之前浏览的页面,则可以使用forward()方法,代码如下: window.hi ...