vue-quill-editor富文本编辑器使用,自定义工具栏,自定义图片上传

1.在项目中安装vue-quill-editor

npm install vue-quill-editor --save

2. 富文本编辑器配置文件

quill-Config.js

//axios相关配置,下面有相关文件
import service from '../../utils/request';
/*富文本编辑图片上传配置*/
const uploadConfig = {
    action: 'oss/service/oss/upload', // 必填参数 图片上传地址
    methods: 'POST', // 必填参数 图片上传方式
    headers: {
        "Content-Type": "multipart/form-data"
    },
    token: '', // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
    name: 'name', // 必填参数 文件的参数名
    size: 10240, // 可选参数   图片大小,单位为Kb, 1M = 1024Kb
    accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon,image/jpg' // 可选 可上传的图片格式
};

// toolbar工具栏的工具选项(默认展示全部)
const toolOptions = [
    ['bold', 'italic', 'underline', 'strike'],
    ['blockquote', 'code-block'],
    [{
        'header': 1
    }, {
        'header': 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'],
    //['link', 'image', 'video']
    ['link', 'image']
];
const handlers = {
    image: function image() {
        var self = this;

        var fileInput = this.container.querySelector('input.ql-image[type=file]');
        if (fileInput === null) {
            fileInput = document.createElement('input');
            fileInput.setAttribute('type', 'file');

            // 设置图片参数名
            if (uploadConfig.name) {
                fileInput.setAttribute('name', uploadConfig.name);
            }
            // 可设置上传图片的格式
            fileInput.setAttribute('accept', uploadConfig.accept);
            fileInput.classList.add('ql-image');
            // 监听选择文件
            fileInput.addEventListener('change', function () {

                // 创建formData
                let formData = new FormData();
                formData.append('file', fileInput.files[0]);
                fileInput.value = '';
                //这里 请求
                service.post('oss/service/oss/upload', formData, {
                    headers: {
                        "Content-Type": "multipart/form-data;"
                    }
                }).then(resp => {
                    var path = resp.data;
                    //var picPath = process.env.FTP_URL + path;
                    let length = self.quill.getSelection(true).index;
                    //这里很重要,你图片上传成功后,img的src需要在这里添加,res.path就是你服务器返回的图片链接。  
                    //console.log(path)
                    self.quill.insertEmbed(length, 'image', path);
                    self.quill.setSelection(length + 1)
                    fileInput.value = ''
                });
            });
            this.container.appendChild(fileInput);
        }
        fileInput.click();
    }
};

export default {
    placeholder: '请输入内容',
    theme: 'snow', // 主题
    modules: {
        toolbar: {
            container: toolOptions, // 工具栏选项
            handlers: handlers // 事件重写
        }
    }
};

utils/request.js

import axios from 'axios';

import {
    Message
} from 'element-ui'
//这就是富文本编辑器中使用到的service,其实就是创建的一个新的axios
const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了,暂时不使用了
    // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
    timeout: 15000
});


//请求拦截
service.interceptors.request.use(
    config => {
        let token = sessionStorage.getItem('token_mark');
        config.headers.token_mark = token;
        return config;
    },
    error => {
        return Promise.reject();
    }
);

//响应拦截
service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            //如果有token的话就放到本地
            if (response.headers.token_mark) {
                sessionStorage.setItem('token_mark', response.headers.token_mark)
            }
            return response.data;
        } else {
            Message({
                showClose: true,
                duration: 0,
                message: '服务异常,请联系管理员',
                type: 'error'
            });
            Promise.reject();
        }
    },
    error => {
        Message({
            showClose: true,
            duration: 0,
            message: '服务异常,请联系管理员',
            type: 'error'
        });
        return Promise.reject();
    }
);
export default service;

3.引入富文本组件,以及配置到项目中

  1. 引入配置和富文本组件

    //富文本框
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import { quillEditor } from 'vue-quill-editor';
    import quillConfig from '../../../assets/js/quill-config';
    import Vue from 'vue';
    
  2. 自定义配置引入

    export default {
      data() {
        return {
          //自定义配置
          editorOption: quillConfig
      }
      }
    
  3. 富文本组件引入

    //组件,与data()平级  
    components: {
        quillEditor
      }
    
  4. 组件使用

    <!--
    富文本中的内容,绑定的属性
    v-model="updateProductDetailModel.introduction" 
    :options="editorOption"
    富文本自定义的属性
    -->
    <quill-editor ref="myTextEditor" v-model="updateProductDetailModel.introduction" :options="editorOption"></quill-editor>
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值