1. PreparationDownload vue-quill-editor npm install vue-quill-editor --save or yarn add vue-quill-editor 2. Define the global component quill-editorAfter downloading vue-quill-editor, we need to define a global component and name it quill-editor 1. Define template<div> <quill-editor v-model="value" ref="myQuillEditor" :options="editorOption" @change="onEditorChange" > </quill-editor> <input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleChange" /> </div> 2. Define rich text option configurationeditorOption: { toolbar: [ ['bold', 'italic', 'underline'], // bold, italic, underline, strikethrough, 'strike' ['blockquote', 'code-block'], //Quote, code block [{ 'header': 1 }, { 'header': 2 }], //H1 H2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : } 3. Related methods1. Change the original rich text editor upload image binding methodmounted() { if (this.$refs.myQuillEditor) { //Change myQuillEditor to your own this.$refs.myQuillEditor.quill.getModule("toolbar").addHandler("image", this.imgHandler); } }, methods:{ imgHandler(state) { if (state) { //Trigger the click of input, replace fileBtn with its own this.$refs.fileBtn.click() } } } 2. Upload eventshandleChange(e) { const files = Array.prototype.slice.call(e.target.files); if (!files) { return; } let formdata = new FormData(); formdata.append("file_name", files[0].name); formdata.append("imgs", files[0]); //Using axios request this.axios({ url: this.$store.state.baseUrl + 'upload/ueditorFile', method: 'post', data: formdata, headers: {'client-identity': localStorage.getItem('session_id')} }).then((res) => { //Setting it to empty here is to trigger the change event when uploading the same picture this.$refs.fileBtn.value = ""; if (res.data.code == 200) { let selection = this.$refs.myQuillEditor.quill.getSelection(); //This is the returned image address. If the interface does not return an accessible address, you need to concatenate it yourself. let imgUrl = this.$store.state.baseUrl + res.data.data; imgUrl = imgUrl.replace(/\\/g,"/") //Get the quill cursor and insert the image this.$refs.myQuillEditor.quill.insertEmbed(selection != null ? selection.index : 0, 'image', imgUrl) //After the insertion is completed, the cursor moves back one position this.$refs.myQuillEditor.quill.setSelection(selection.index + 1); } }) } Finally, use this global quill component in the parent component and pass the relevant parameters you need, and you're done~ This is the end of this article about using vue-quill-editor rich text editor in vue and uploading pictures to the server. For more relevant content about vue-quill-editor uploading pictures to the server, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementing a simple student information management system based on VUE
>>: Vue implements an Input component that gets the key display shortcut key effect
This article briefly describes how to use Docker ...
MySQL Views Simply put, a MySQL view is a shortcu...
Table of contents 1. Vue3 component communication...
Preface Recently, I have been helping clients con...
If you want to adjust the size and number of Inno...
Why use Server-Side Rendering (SSR) Better SEO, s...
Preface We need to retrieve certain data that mee...
This article shares with you two methods of setti...
Table of contents 1. Problem 2. Solution 1. Decla...
In order to prevent non-compliant data from enter...
Note: It is recommended that the virtual machine ...
Table of contents 1. Maven Dependency 2. Menu rel...
Table of contents Code: Replenish: Summarize Requ...
When making a form in a recent project, I need to...
Table of contents 1. Introduction 2. Use 1. Diffe...