电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

淺談vue中使用編輯器vue-quill-editor踩過的坑

瀏覽:114日期:2022-12-19 11:37:48

結合vue+element-ui+vue-quill+editor二次封裝成組件

1.圖片上傳

分析原因

項目中使用vue-quill-editor富文本編輯器,在編輯內容的時候,我們往往會編輯圖片,而vue-quill-editor默認的處理方式是直接將圖片轉成base64格式,導致上傳的內容十分龐大,且服務器接受post的數據的大小是有限制的,很有可能就提交失敗,造成用戶體驗差。

引入element-ui

編輯editor.vue文件

<template> <div> <!-- 圖片上傳組件輔助--> <el-upload action='' accept='image/jpg, image/jpeg, image/png, image/gif' :http-request='upload' :before-upload='beforeUploadImg' :on-success='uploadSuccess' :on-error='uploadError' :show-file-list='false'> <i class='el-icon-plus avatar-uploader-icon'></i> </el-upload> </div></template><script> import axios from ’@/API/’;import { quillEditor } from 'vue-quill-editor';import COS from 'cos-js-sdk-v5';import Upload from ’@/components/Upload.vue’;import { addQuillTitle } from ’../utils/quill-title.js’;import 'quill/dist/quill.core.css';import 'quill/dist/quill.snow.css';import 'quill/dist/quill.bubble.css'; export default { data() { return { } }, methods: { // 上傳圖片前 beforeUpload(res, file) { const isJPG = file.type === ’image/jpg’ || file.type === ’image/png’ || file.type === ’image/jpeg’ const isLt1M = file.size / 1024 / 1024 < 1 if (!isJPG) { this.$message.error(’支持JPG、PNG格式的圖片,大小不得超過1M’) } if (!isLt1M) { this.$message.error(’文件最大不得超過1M’) } return isJPG && isLt1M }, // 上傳圖片成功 uploadSuccess(res, file) {}, // 上傳圖片失敗 uploadError(res, file) {}, // 上傳圖片處理過程 upload(req){} } }</script>

在editor.vue中引入vue-quill-editor

<template> <div> <!-- 圖片上傳組件輔助--> <el-upload action='' accept='image/jpg, image/jpeg, image/png, image/gif' :http-request='upload' :before-upload='beforeUploadImg' :on-success='uploadSuccess' :on-error='uploadError' :show-file-list='false'> <i class='el-icon-plus avatar-uploader-icon'></i> </el-upload> <quill-editor v-model='content' ref='QuillEditor' :options='editorOption' @blur='onEditorBlur($event)' @focus='onEditorFocus($event)' @ready='onEditorReady($event)'> </quill-editor> </div></template><script> import axios from ’@/API/’;import { quillEditor } from 'vue-quill-editor';import COS from 'cos-js-sdk-v5';import Upload from ’@/components/Upload.vue’;import { addQuillTitle } from ’../utils/quill-title.js’;import 'quill/dist/quill.core.css';import 'quill/dist/quill.snow.css';import 'quill/dist/quill.bubble.css'; // 工具欄配置 const toolbarOptions = [ [’bold’, ’italic’, ’underline’, ’strike’], // toggled buttons [’blockquote’, ’code-block’], [{’header’: 1}, {’header’: 2}],// custom button values [{’list’: ’ordered’}, {’list’: ’bullet’}], [{’script’: ’sub’}, {’script’: ’super’}], // superscript/subscript [{’indent’: ’-1’}, {’indent’: ’+1’}], // outdent/indent [{’direction’: ’rtl’}], // text direction [{’size’: [’small’, false, ’large’, ’huge’]}], // custom dropdown [{’header’: [1, 2, 3, 4, 5, 6, false]}], [{’color’: []}, {’background’: []}], // dropdown with defaults from theme [{’font’: []}], [{’align’: []}], [’link’, ’image’, ’video’], [’clean’] // remove formatting button] export default { data() { return { editorOption: { placeholder: ’請輸入編輯內容’, theme: ’snow’, //主題風格 modules: { toolbar: { container: toolbarOptions, // 工具欄 handlers: { ’image’: function (value) { if (value) { document.querySelector(’#quill-upload input’).click() } else { this.quill.format(’image’, false); } } } } } }, // 富文本編輯器配置 content: ’’, //富文本內容 } }, methods: { // 上傳圖片前 beforeUpload(res, file) { const isJPG = file.type === ’image/jpg’ || file.type === ’image/png’ || file.type === ’image/jpeg’ const isLt1M = file.size / 1024 / 1024 < 1 if (!isJPG) { this.$message.error(’支持JPG、PNG格式的圖片,大小不得超過1M’) } if (!isLt1M) { this.$message.error(’文件最大不得超過1M’) } return isJPG && isLt1M }, // 上傳圖片成功 uploadSuccess(res, file) { let quill = this.$refs.QuillEditor.quill; let length = quill.getSelection().index; quill.insertEmbed(length, ’image’, url); quill.setSelection(length+1) }, // 上傳圖片失敗 uploadError(res, file) {this.$message.error(’圖片插入失敗’) }, // 上傳圖片處理過程 upload(req){} } }</script><style scoped>.avatar-uploader{display: none;}</style>

2.編輯器上增加title提示

在編輯器上增加一個quill-title.js的工具欄的title的配置文件

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-list’:’列表’, ’ql-align’:’文本對齊’, ’ql-direction’:’文本方向’, ’ql-code-block’:’代碼塊’, ’ql-formula’:’公式’, ’ql-image’:’圖片’, ’ql-video’:’視頻’, ’ql-clean’:’清除字體樣式’};export function addQuillTitle(){ const oToolBar = document.querySelector(’.ql-toolbar’), aButton = oToolBar.querySelectorAll(’button’), aSelect = oToolBar.querySelectorAll(’select’), aSpan= oToolBar.querySelectorAll(’span’); aButton.forEach((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 if(item.className === ’ql-list’){ item.value===’ordered’ ? item.title=’有序列表’ : item.title=’無序列表’ }else if(item.className === ’ql-header’){ item.value === ’1’ ? item.title = ’標題H1’: item.title = ’標題H2’; }else{ item.title = titleConfig[item.classList[0]]; } }); aSelect.forEach((item)=>{ if(item.className!=’ql-color’&&item.className!=’ql-background’){ item.parentNode.title = titleConfig[item.classList[0]]; } }); aSpan.forEach((item)=>{ if(item.classList[0]===’ql-color’){ item.title = titleConfig[item.classList[0]]; }else if(item.classList[0]===’ql-background’){ item.title = titleConfig[item.classList[0]]; } });}

在editor.vue中引入quill-title.js

<template> <div> <!-- 圖片上傳組件輔助--> <el-upload action='' accept='image/jpg, image/jpeg, image/png, image/gif' :http-request='upload' :before-upload='beforeUploadImg' :on-success='uploadSuccess' :on-error='uploadError' :show-file-list='false'> <i class='el-icon-plus avatar-uploader-icon'></i> </el-upload> <quill-editor v-model='content' ref='QuillEditor' :options='editorOption' @blur='onEditorBlur($event)' @focus='onEditorFocus($event)' @ready='onEditorReady($event)'> </quill-editor> </div></template><script> import axios from ’@/API/’;import { quillEditor } from 'vue-quill-editor';import COS from 'cos-js-sdk-v5';import Upload from ’@/components/Upload.vue’;import { addQuillTitle } from ’../utils/quill-title.js’;import 'quill/dist/quill.core.css';import 'quill/dist/quill.snow.css';import 'quill/dist/quill.bubble.css'; // 工具欄配置 const toolbarOptions = [ [’bold’, ’italic’, ’underline’, ’strike’], // toggled buttons [’blockquote’, ’code-block’], [{’header’: 1}, {’header’: 2}],// custom button values [{’list’: ’ordered’}, {’list’: ’bullet’}], [{’script’: ’sub’}, {’script’: ’super’}], // superscript/subscript [{’indent’: ’-1’}, {’indent’: ’+1’}], // outdent/indent [{’direction’: ’rtl’}], // text direction [{’size’: [’small’, false, ’large’, ’huge’]}], // custom dropdown [{’header’: [1, 2, 3, 4, 5, 6, false]}], [{’color’: []}, {’background’: []}], // dropdown with defaults from theme [{’font’: []}], [{’align’: []}], [’link’, ’image’, ’video’], [’clean’] // remove formatting button] export default { data() { return { editorOption: { placeholder: ’請輸入編輯內容’, theme: ’snow’, //主題風格 modules: { toolbar: { container: toolbarOptions, // 工具欄 handlers: { ’image’: function (value) { if (value) { document.querySelector(’#quill-upload input’).click() } else { this.quill.format(’image’, false); } } } } } }, // 富文本編輯器配置 content: ’’, //富文本內容 } }, mounted(){ addQuillTitle(); }, methods: { // 上傳圖片前 beforeUpload(res, file) { const isJPG = file.type === ’image/jpg’ || file.type === ’image/png’ || file.type === ’image/jpeg’ const isLt1M = file.size / 1024 / 1024 < 1 if (!isJPG) { this.$message.error(’支持JPG、PNG格式的圖片,大小不得超過1M’) } if (!isLt1M) { this.$message.error(’文件最大不得超過1M’) } return isJPG && isLt1M }, // 上傳圖片成功 uploadSuccess(res, file) { let quill = this.$refs.QuillEditor.quill; let length = quill.getSelection().index; quill.insertEmbed(length, ’image’, url); quill.setSelection(length+1) }, // 上傳圖片失敗 uploadError(res, file) {this.$message.error(’圖片插入失敗’) }, // 上傳圖片處理過程 upload(req){} } }</script><style scoped>.avatar-uploader{display: none;}</style>

補充知識:Vue引用quill富文本編輯器,圖片處理的兩個神器插件(quill-image-drop-module、quill-image-resize-module)的正確姿勢。(解決各種報錯)

一、前言

我在vue-quill-editor的Github認識了這兩個插件。

quill-image-drop-module:允許粘貼圖像并將其拖放到編輯器中。

quill-image-resize-module:允許調整圖像大小。

都很實用吶!

然而DEMO不夠詳細,實際引用時,報了很多錯誤。

Cannot read property ’imports’ of undefined'、

Failed to mount component: template or render function not defined.、

Cannot read property ’register’ of undefined。

下面說一下正確的引用姿勢。

二、我的環境

Webpack + Vue-Cli 2.0 (vue init非simple的版本)

三、正文

1、確保你的quill富文本編輯器(不添加插件時)可以正常運行。

2、安裝NPM依賴。

cnpm install quill-image-drop-module -S

cnpm install quill-image-resize-module -S

3、在build文件夾下找到webpack.base.conf.js。

修改:

module.exports = { plugins: [ new webpack.ProvidePlugin({ // 在這兒添加下面兩行 ’window.Quill’: ’quill/dist/quill.js’, ’Quill’: ’quill/dist/quill.js’ }) ]}

4、修改你在頁面引用的“quill富文本組件”。

修改<script>標簽內代碼:

// 以前需要有下面幾行: import { quillEditor } from ’vue-quill-editor’ //注意quillEditor必須加大括號,否則報錯。 import 'quill/dist/quill.core.css';// import 'quill/dist/quill.snow.css'; // 新增下面代碼: import resizeImage from ’quill-image-resize-module’ // 調整大小組件。 import { ImageDrop } from ’quill-image-drop-module’; // 拖動加載圖片組件。 Quill.register(’modules/imageDrop’, ImageDrop); Quill.register(’modules/resizeImage ’, resizeImage )

然后,修改頁面引用的:options='editorOption'。

editorOption: { modules: { // 新增下面 imageDrop: true, // 拖動加載圖片組件。 imageResize: { //調整大小組件。 displayStyles: { backgroundColor: ’black’, border: ’none’, color: ’white’ }, modules: [ ’Resize’, ’DisplaySize’, ’Toolbar’ ] } }}

重新 npm run dev ,插件運行成功!

以上這篇淺談vue中使用編輯器vue-quill-editor踩過的坑就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 自动焊锡机_点胶机_螺丝机-锐驰机器人 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 算命免费_生辰八字_免费在线算命 - 卜算子算命网 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | arch电源_SINPRO_开关电源_模块电源_医疗电源-东佑源 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 聚合甘油__盐城市飞龙油脂有限公司 | 品牌策划-品牌设计-济南之式传媒广告有限公司官网-提供品牌整合丨影视创意丨公关活动丨数字营销丨自媒体运营丨数字营销 | 柔性输送线|柔性链板|齿形链-上海赫勒输送设备有限公司首页[输送机] | 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 设定时间记录电子秤-自动累计储存电子秤-昆山巨天仪器设备有限公司 | 东莞市海宝机械有限公司-不锈钢分选机-硅胶橡胶-生活垃圾-涡电流-静电-金属-矿石分选机 | IWIS链条代理-ALPS耦合透镜-硅烷预处理剂-上海顶楚电子有限公司 lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | NBA直播_NBA直播免费观看直播在线_NBA直播免费高清无插件在线观看-24直播网 | 卸料器-卸灰阀-卸料阀-瑞安市天蓝环保设备有限公司 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 银川美容培训-美睫美甲培训-彩妆纹绣培训-新娘化妆-学化妆-宁夏倍莱妮职业技能培训学校有限公司 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 天坛家具官网| 螺杆真空泵_耐腐蚀螺杆真空泵_水环真空泵_真空机组_烟台真空泵-烟台斯凯威真空 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 高压油管,液压接头,液压附件-烟台市正诚液压附件 | 福州甲醛检测-福建室内空气检测_环境检测_水质检测-福建中凯检测技术有限公司 | 合肥卓创建筑装饰,专业办公室装饰、商业空间装修与设计。 | 专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】 | 美甲贴片-指甲贴片-穿戴美甲-假指甲厂家--薇丝黛拉 | 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 气动球阀_衬氟蝶阀_调节阀_电动截止阀_上海沃托阀门有限公司 | 北京网站建设|北京网站开发|北京网站设计|高端做网站公司 | 智慧旅游_智慧景区_微景通-智慧旅游景区解决方案提供商 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 天坛家具官网 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 汕头市盛大文化传播有限公司,www.11400.cc |