1、npm 安装 vue-quill-editor

npm install vue-quill-editor

2、再main.js中引入

import  VueQuillEditor from 'vue-quill-editor'
// require styles 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css' Vue.use(VueQuillEditor)

3、创建子组件UE.vue

<template>
<div>
<Upload
:show-upload-list="false"
class="editor_upload"
:format="['jpg','jpeg','png']"
:max-size="2048"
multiple
:before-upload="handleUpload"
action="//jsonplaceholder.typicode.com/posts/"
>
<Button icon="ios-cloud-upload-outline" ></Button>
</Upload>
<quill-editor
v-model="msg"
:options="editorOption"
ref="QuillEditor">
</quill-editor>
</div>
</template>
<script>
import {PostBase64Upload} from '../../../services/pages/market'
//工具
const toolbarOptions = [
['bold', 'italic', 'underline', 'strike','blockquote', 'code-block',{'indent': '-1'}, {'indent': '+1'},{'color': []}, {'background': []},{'align': []},'image','clean'], // toggled buttonssuperscript/subscript
];
//工具名称
const titleConfig = {
'ql-bold':'加粗',
'ql-color':'颜色',
'ql-font':'字体',
'ql-code':'插入代码',
'ql-italic':'斜体',
'ql-link':'添加链接',
'ql-background':'背景颜色',
'ql-size':'字体大小',
'ql-strike':'删除线',
'ql-script':'上标/下标',
'ql-underline':'下划线',
'ql-blockquote':'引用',
'ql-header':'标题',
'ql-indent':'缩进',
'ql-align':'文本对齐',
'ql-direction':'文本方向',
'ql-code-block':'代码块',
'ql-image':'图片',
'ql-video':'视频',
'ql-clean':'清除字体样式',
};
export default {
name: 'UE',
data () {
return {
IMAGE_URL: global.IMG_URL,
QuillEditor: null,
editorOption: {
modules: {
toolbar: {
container: toolbarOptions, // 工具栏
handlers: {
'image': function (value) {
if (value) {
document.querySelector('.ivu-upload .ivu-btn').click()
} else {
this.quill.format('image', false);
}
}
}
}
}
},
file_url: '',
importFile: '',
msg: ''
}
},
props: ["defaultMsg",],
mounted() {
this.msg = this.defaultMsg;
this.addQuillTitle()
},
methods: {
// 提交文件并阻断自动上传
handleUpload(file) {
let that = this;
let time = setTimeout(function () {
that.upload(file);
clearTimeout(time);
}, 0);
return false
},
//上传图片
upload(file) {
let index = file.name.lastIndexOf("\.");
let file_url = file.name.substring(index + 1, file.name.length);
if (file_url == 'jpg' || file_url == 'jpeg' || file_url == 'png' ) {
let that = this;
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
PostBase64Upload({
base64_content: e.target.result,
module_name: 'topic'
})
.then(data => {
that.file_url = data.file_url;
that.handleSuccess(that.file_url)
})
.catch(err => {
that.$Message.error(err);
});
return false;
};
} else {
this.$Message.error("请上传'jpg','jpeg', png'格式的图片");
return false;
}
}, //图片加入到富文本框中
handleSuccess (res) {
let quill = this.$refs.QuillEditor.quill;
let length = quill.getSelection().index;
quill.insertEmbed(length, 'image', res)
quill.setSelection(length + 1);
this.msg = this.getImgSrc(this.msg);
},
//加入路径的IP
getImgSrc(content) {
// 正则替换img的路径
let that = this;
content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
content = content.replace(new RegExp(capture, 'g'), that.IMAGE_URL + capture);//这里也可以替换成需要的路径
});
content = content.replace(/<img/g, '<img style="width: 100%;"');
return content;
}, //富文本工具添加名称
addQuillTitle () {
const oToolBar = document.querySelector('.ql-toolbar'),
aButton = oToolBar.querySelectorAll('button'),
aSelect = oToolBar.querySelectorAll('select');
aButton.forEach(function(item){
if(item.className === 'ql-script'){
item.value === 'sub' ? item.title = '下标': item.title = '上标';
}else if(item.className === 'ql-indent'){
item.value === '+1' ? item.title ='向右缩进': item.title ='向左缩进';
}else{
item.title = titleConfig[item.classList[0]];
}
});
aSelect.forEach(function(item){
item.parentNode.title = titleConfig[item.classList[0]];
});
},
},
}
</script>
<style>
.editor_upload {
display: none;
}
</style>

4、引入该组件页面

 <UE :defaultMsg=defaultMsg ref="ue"></UE>

<script>
import UE from "./UE"
//因子组件不能修改父组件的参数,所以父组件提交时,直接调取子组件的参数
this.$refs.ue.msg
</script>

上述的工具栏是根据我司的要求来选择的,如果你想需求的更多,我也是查看别的大神的,你也可以看看https://blog.csdn.net/div_ma/article/details/79536634

vue-quill-editor + iview 实现富文本编辑器及图片上传的更多相关文章

  1. uedit富文本编辑器及图片上传控件

    微力后台 uedit富文本编辑器及文件上传控件的使用,无时间整理,暂略,参考本地代码.能跑起来.

  2. 关于移动手机端富文本编辑器qeditor图片上传改造

    日前项目需要在移动端增加富文本编辑,上网找了下,大多数都是针对pc版的,不太兼容手机,当然由于手机屏幕小等原因也限制富文本编辑器的众多强大功能,所以要找的编辑器功能必须是精简的. 找了好久,发现qed ...

  3. 改造百度UMeditor(UEditor-min)富文本编辑器的图片上传功能

    最近项目需要新增一个发布文章的模块,用的是百度的Ueditor富文本编辑器. 公司用的是阿里云的图片服务器,需要直接把文章中图片上传到服务器上,但是这个编辑器的上传图片是直接上传到Tomcat的根目录 ...

  4. 在Asp.Net Core中配置使用MarkDown富文本编辑器实现图片上传和截图上传(开源代码.net core3.0)

    我们的富文本编辑器不能没有图片上传尤其是截图上传,下面我来教大家怎么实现MarkDown富文本编辑器截图上传和图片上传. 1.配置编辑器到html页 <div id="test-edi ...

  5. wysiwyg 富文本编辑器(附带图片上传功能)

    Fist: 需要的文件 font 文件夹下面的也是需要的哟 Then: 引入文件 <link href="bootstrap/css/bootstrap.css" rel=& ...

  6. Asp.Net Core中配置使用Kindeditor富文本编辑器实现图片上传和截图上传及文件管理和上传(开源代码.net core3.0)

    KindEditor使用JavaScript编写,可以无缝的于Java..NET.PHP.ASP等程序接合. KindEditor非常适合在CMS.商城.论坛.博客.Wiki.电子邮件等互联网应用上使 ...

  7. UEditor富文本编辑器的图片上传 http://fex.baidu.com/ueditor/#server-deploy

    http://fex.baidu.com/ueditor/#server-deploy http://fex.baidu.com/ueditor/#server-path 首先 editor配置文件中 ...

  8. 富文本vue-quill-editor修改图片上传方法

    富文本vue-quill-editor修改图片上传方法 HTML 代码 HTML codes <!-- 上传的组件 --> <upload style="display:n ...

  9. 富文本编辑器handyeditor,上传和预览图片的host地址不一样

    使用富文本编辑器(官网)时,大多时候都会用到图片上传,但是下载的富文本编辑器的默认配置是只有一个上传地址的host的. var he = HE.getEditor('editor', { autoHe ...

随机推荐

  1. 报错:Sqoop2 Error message: Class not found JDBC Driver Class: com.mysql.jdbc.Driver

    报错背景: CDH安装完成Sqoop2的组建后进行创建link的操作. 报错现象: There are issues with entered data, please revise your inp ...

  2. 【计算机视觉】图像配准(Image Registration)

    (Source:https://blog.sicara.com/image-registration-sift-deep-learning-3c794d794b7a)  图像配准方法概述 图像配准广泛 ...

  3. Python3之内建模块itertools

    python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个无限迭代器 >>> import itertools > ...

  4. 【ARTS】01_44_左耳听风-201900909~201900915

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  5. APP排查内存泄漏最简单和直观的方法

        内存泄漏无疑会严重影响用户体验,一些本应该废弃的资源和对象无法被释放,导致手机内存的浪费,app使用的卡顿,那么如何排查内存泄漏呢? 当然,首先我们有google的官方文档可以参考,大部分博客 ...

  6. RabbitMQ官方教程二 Work Queues(GOLANG语言实现)

    RabbitMQ官方教程二 Work Queues(GOLANG语言实现) 在第一个教程中,我们编写了程序来发送和接收来自命名队列的消息. 在这一部分中,我们将创建一个工作队列,该队列将用于在多个wo ...

  7. Linux 缺少 mime.types 文件 mailcap

    问题描述: 一个项目当中使用的是 ossfs 挂载的一个 oss,在系统上传附件时,比如图片或视频时, 它的头信息为,application/octet-stream,上传后直接为二进制文件,访问的话 ...

  8. 036 Android Xutils3网络请求框架使用

    1.xUtils3 介绍 xUtils 中目前包括了主要的四大模块,分别为 DbUtils 模块.ViewUtils 模块.HttpUtils 模块以及 BitmapUtils 模块. xUtils3 ...

  9. vue导入css,js和放置html代码

    使用场景:我是从网上找的html前端页面模板,导入找的模板中的css和js到vue中使用. 1.在main.js中全局导入css和js import '@/assets/css/main.css' i ...

  10. c++实现双端队列

    在使用c++容器的时候其底层如何实现  例如  vector 容器  :是一个内存可以二倍扩容的向量容器,使用方便但是对内存要求严格,弊端明显    list  容器  : 双向循环链表    deq ...