vue-quill-editor 图片上传处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | <template> <div class = "quill-editor" > <!-- 图片上传组件辅助--> <el-upload class = "avatar-uploader" :action= "uploadUrl" name= "img" :show-file-list= "false" :on-success= "uploadSuccess" :before-upload= "beforeUpload" > </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 v-html= "content" /> </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' ; 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 ] }], // 标题 [{ 'color' : [] }, { 'background' : [] }], // 颜色选择 [{ 'font' : [ 'SimSun' , 'SimHei' , 'Microsoft-YaHei' , 'KaiTi' , 'FangSong' , 'Arial' ] }], // 字体 [{ 'align' : [] }], // 居中 [ 'clean' ], // 清除样式, [ 'link' , 'image' ], // 上传图片、上传视频 ] export default { components: { quillEditor }, data () { return { name: 'register-modules-example' , content: '' , editorOption: { placeholder: '请在这里输入' , theme: 'snow' , //主题 snow/bubble modules: { history: { delay: 1000, maxStack: 50, userOnly: false }, toolbar: { container: toolbarOptions, handlers: { image: function (value) { if (value) { // 调用element的图片上传组件 document.querySelector( '.avatar-uploader input' ).click() } else { this .quill.format( 'image' , false ) } } } } } }, uploadUrl: `XXXXXXXX` // 服务器上传地址 } }, methods: { // 失去焦点 onEditorBlur (editor) { }, // 获得焦点 onEditorFocus (editor) { }, // 开始 onEditorReady (editor) { }, // 值发生变化 onEditorChange (editor) { this .content = editor.html; console.log(editor); }, beforeUpload (file) { }, uploadSuccess (res) { // 获取富文本组件实例 let quill = this .$refs.QuillEditor.quill // 如果上传成功 if (res) { // 获取光标所在位置 let length = quill.getSelection().index; // 插入图片,res为服务器返回的图片链接地址 quill.insertEmbed(length, 'image' , res.result.url) // 调整光标到最后 quill.setSelection(length + 1) } else { // 提示信息,需引入Message this .$message.error( '图片插入失败!' ) } } } } </script> |
原文链接 https://blog.csdn.net/HH18700418030/article/details/119999730
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?