問題描述:富文本框復制粘貼未走上傳圖片接口,會將復制的圖片解析為base64編碼,為了控制這種情況可選擇禁用粘貼圖片,或者監聽有復制粘貼的圖片走上傳圖片接口
-
獲取到?
quill
?對象,可以通過?refs
?或者?Quill
?對象的?getInstance()
?方法獲取。 -
注冊?
paste
?事件處理函數,通過事件對象的?clipboardData
?屬性獲取剪切板中的內容,判斷是否存在圖片,如果存在則阻止默認行為。 -
最后在組件銷毀時需要記得解除事件處理函數。
<template><div><quill-editorref="editor"v-model="content":options="{modules: {toolbar: [['bold', 'italic', 'underline', 'strike'], ['link', 'image', 'video']]},placeholder: '請輸入內容...',}"></quill-editor></div>
</template><script>
import { QuillEditor } from 'vue-quill-editor'; export default {name: 'MyComponent',components: { QuillEditor },data() {return {content: '',quill: null};},mounted() {this.quill = this.$refs.editor.quill;this.quill.root.addEventListener('paste', this.handlePaste, false);},beforeDestroy() {this.quill.root.removeEventListener('paste', this.handlePaste, false);},methods: {handlePaste(e) {const clipboardData = e.clipboardData;const types = clipboardData.types;if (types.includes('Files')) {// 禁止粘貼圖片e.preventDefault();}},},
};
</script>