Vue uses vue-quill-editor rich text editor and uploads pictures to the server

Vue uses vue-quill-editor rich text editor and uploads pictures to the server

1. Preparation

Download vue-quill-editor

npm install vue-quill-editor --save or yarn add vue-quill-editor

2. Define the global component quill-editor

After 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 configuration

editorOption: {
 toolbar: [
 ['bold', 'italic', 'underline'], // bold, italic, underline, strikethrough, 'strike'
 ['blockquote', 'code-block'], //Quote, code block [{ 'header': 1 }, { 'header': 2 }], //H1 H2
 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
}

3. Related methods

1. Change the original rich text editor upload image binding method

mounted() {
 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 events

handleChange(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:
  • Vue implements attachment upload function
  • Spring+Vue integrates UEditor rich text to upload picture attachments
  • Two solutions for Vue package upload server refresh 404 problem
  • Vue+element+oss realizes front-end fragment upload and breakpoint resume
  • Based on vue-simple-uploader, encapsulate the global upload plug-in function of file segment upload, instant upload and breakpoint resume
  • Vue form post request combined with Servlet to realize file upload function
  • Realize mobile image upload, compression, drag-and-drop sorting, and drag-and-drop deletion functions based on Vue2
  • Things to note when uploading pictures with vue+vant
  • Vue realizes uploading after cropping pictures
  • Implementation example of uploading multiple attachments in Vue

<<:  Implementing a simple student information management system based on VUE

>>:  Vue implements an Input component that gets the key display shortcut key effect

Recommend

Detailed explanation of deploying MySQL using Docker (data persistence)

This article briefly describes how to use Docker ...

Detailed explanation of the usage and differences of MySQL views and indexes

MySQL Views Simply put, a MySQL view is a shortcu...

Details of 7 kinds of component communication in Vue3

Table of contents 1. Vue3 component communication...

Introduction to Nginx regular expression related parameters and rules

Preface Recently, I have been helping clients con...

InnoDB engine redo file maintenance method

If you want to adjust the size and number of Inno...

Detailed explanation of Vue's SSR server-side rendering example

Why use Server-Side Rendering (SSR) Better SEO, s...

An article to understand the execution process of MySQL query statements

Preface We need to retrieve certain data that mee...

How to set horizontal navigation structure in Html

This article shares with you two methods of setti...

TypeScript problem with iterating over object properties

Table of contents 1. Problem 2. Solution 1. Decla...

Methods and steps for deploying GitLab environment based on Docker

Note: It is recommended that the virtual machine ...

Detailed explanation of vue page state persistence

Table of contents Code: Replenish: Summarize Requ...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...