天天看點

vue使用富文本編輯器vue-quill-editor,含拖拽圖檔,調整預設高度

這兩天的工作用到了vue-quill-editor,來記錄一下使用心得/踩過的坑 供大家參考。

1、安裝

npm install vue-quill-editor

npm install quill

2、引入

main.js裡樣式也記得引入:

import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'      
Vue.use(VueQuillEditor)      

 在所用頁面引入編輯器:

import { quillEditor } from 'vue-quill-editor'
import Quill from 'quill' //引入編輯器      

 記得括号

3、配置

編輯器預設的toolbar可用選項非常多

vue使用富文本編輯器vue-quill-editor,含拖拽圖檔,調整預設高度

有些用不上,就可以随意控制toolbar部分的可用選項,配置如下:

// 不需要的的選項在這裡删掉就好
const toolbarOptions = [
   ['bold', 'italic', 'underline', 'strike'],    //加粗,斜體,下劃線,删除線
   ['blockquote', 'code-block'],     //引用,代碼塊
  
   [{ 'header': 1 }, { 'header': 2 }],        // 标題,鍵值對的形式;1、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': [] }],     //字型
   [{ 'align': [] }],    //對齊方式
  
   ['clean'],    //清除字型樣式
   ['image','video']    //上傳圖檔、上傳視訊
]

export default {
    data (){
        return {
            content: '', // 富文本編輯器預設内容
            editorOption: { //  富文本編輯器配置
                modules: {
                    toolbar: {
                        container: toolbarOptions, // 工具欄
                    },
                },
                placeholder: '請輸入正文...'
            },
        }
    }
}      

 頁面引入元件:

<quill-editor
        v-model="content"
        ref="myQuillEditor"
        :options="editorOption"
        @blur="onEditorBlur($event)"
        @focus="onEditorFocus($event)"
        @change="onEditorChange($event)">
</quill-editor>
<button v-on:click="saveHtml">實時預覽</button>      

函數部分:

onEditorReady(editor) {}, // 準備編輯器
onEditorBlur(){}, // 失去焦點事件
onEditorFocus(){}, // 獲得焦點事件
onEditorChange(){}, // 内容改變事件
saveHtml:function(event){
    console.log(event)
},      

 4、注意事項

1、剪輯器預設的高度隻有一行文字,這不太好

vue使用富文本編輯器vue-quill-editor,含拖拽圖檔,調整預設高度

如果想要把輸入框調高隻需一行: 

<style>

    .ql-editor{

        height:360px;

    }

​​​​​​​</style>

但一定注意,必須設為全局的樣式才能生效,是以去掉scoped!!!!

2、編輯器預設點選上傳圖檔隻能單張上傳,如果想要使用更友善,可以使用插件quill-image-drop-module,就可多張拖拽上傳。

安裝:npm install quill-image-drop-module

在所用頁面引入:

import { ImageDrop } from 'quill-image-drop-module';
Quill.register('modules/imageDrop', ImageDrop);
      

在editorOption的modules裡配置:imageDrop:true,

3、編輯器上傳的圖檔不可以編輯控制大小,查到一個插件quill-image-resize-module可以調節圖檔大小

奈何百度了各種方法,也配置了webpack.base.conf 中的module.rules子產品,但依舊

Cannot read property 'imports' of undefined"

Cannot read property 'register' of undefined

還有說最後壓縮打包也會出錯,,,遂棄用-_-

改為上傳至oss請求圖檔時再控制大小

4、如果在安裝時遇到報錯errno-4048,提示Please try running this command again as root\Administrator.

已經使用管理者身份打開安裝還是會報錯,,,清除npm緩存還是不行,,,搞了好久,最終解決:

關掉目前工程,不要在idea或什麼打開狀态,關掉!然後去cmd裡面,

更新npm install -g [email protected],然後再去install 插件就好了!!!

(關掉工程然後直接安裝插件可能也行,但我是先更新了一下npm,小夥伴們可以試試)

就先記錄這麼多,正在結合element-ui來使上傳圖檔時直接到oss,開發完成在來記錄~~~