天天看点

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,开发完成在来记录~~~